views:

486

answers:

2
A: 

you can surround the for loops with an if statement if any of your lists are empty.

For example,

{% if technology_list %}
    {% for technology in technology_list %} {{ technology.technology }} {% endfor %}
{% endif %}

Lastly, in the view, you can check to see the posted item is in the request dictionary.

if 'technology' in request.POST:
    tech = request.POST['technology']

-----Update-----

First off, you should probably be using django forms to do this. Secondly, you should be using stackoverflow to ask specific questions. You shouldn't be using it as a method to fix your code.

Lastly, the error seems to be thrown for the following line:

pathology_id = request.GET['pathology_id']

The problem is that the key 'pathology_id' isn't inside the GET dictionary. If you have a default value for this field, you can do something like this:

pathology_id = 'default_value'
if 'pathology_id' in request.GET:
    pathology_id = request.GET['pathology_id']

Otherwise, if you don't have a default value, you obviously can't get an Pathology object without an ID. You will then have to handle the case when nothing is selected.

if 'pathology_id' in request.GET:
    pathology_id = request.GET['pathology_id']

    #rest of your code

else:
    #code when no pathology_id is selected
Eldila
A: 

Neither solution works. The problem is that when nothing is selected I think the entire list is being sent to the view, so it sees an array coming and that gives me the error. Is there some way to force the blank option to be highligted. When I do selected="selected" all the records are highlighted.

{% for commodity in commodity_list %} {{ commodity.commodity }} {% endfor %}

I would also like to give the user the option to choose multiple selections. I don't know how to write the view to accept that. This is the code for accepting one highlighted record. I think getting the code to work with muliple selections may solve my other problem?

pathology_id = request.GET['pathology_id']
p = get_object_or_404(Pathology, pk=pathology_id) pub1p=Publication.objects.exclude(pathpubcombo__pathology__id = 1).exclude(activeyesno = 2).filter(pathpubcombo__pathology=p)

Sorry, the select is not printing on the message:<select id="pathology_id" name="pathology_id" size="6"><option line with select="selected" end of option line></select>