views:

79

answers:

4

Template:

<form method="POST" action="/Bycategory/">
<input type="radio" name="andor1" value=1 checked>
<input type="radio" name="andor1" value=2>
<select id="pathology_id" name="pathology_id">
    {% for pathology in pathology_list %}
        <option value="{{ pathology.id }}">{{ pathology.pathology }}</option>
    {% endfor %}
</select>

There are actually three search selects(pathology, commodity, technology) The user can do and/or for a mix or match of the three, which is why I need the and/or option in the views.py.

The views.py:

def Bypub(request):
    andor1 = request.POST['andor1']
    pathology_id = request.POST['pathology_id']
    p = get_object_or_404(Pathology, pk=pathology_id)
    pub1=Publication.objects.exclude(pathpubcombo__pathology__id= 1).filter(pathpubcombo__pathology=p)
    list=[]
    andlist=[]
    for publication in pub1:
        if andor1 == 1:
            if publication not in list:
                list.append(publication)
        if andor1 == 2:
            if publication in list:
                andlist.append(publication)
                #list=andlist
    return render_to_response('search/categories.html', {
        'andor1' : andor1,
        'pub1': pub1,
        'pathology': p,
        'list' : list,
        'andlist' : andlist,
    },
        context_instance=RequestContext(request)
    )

I know that all my code works without error, but the line (if andor1 ==1:) and (if andor1 ==2:) is being ignored. I suspect that the value for andor1 does not appear at the point where I'm using it. I think it doesn't actually render until after the return render_to_response, because it appears in the next template as a value, otherwise I would see some kind of response at the if andor1 ==1: in the template. Any suggestions?

A: 

The dpaste code: http://dpaste.com/10899/

Please edit your question: Use an indentation of 4 spaces for code examples.
Ferdinand Beyer
You can easily achieve this by selecting the code in the EDIT text field and hitting Ctrl+K
Ferdinand Beyer
A: 

It's not clear what this model and view are meant to do. Could you give us some more context, please?

Daniel Watkins
+1  A: 

The value of andor1 is a string when passed from the HTML form and "1" == 1 is False in Python. Try the following:

try:
    andor1 = int(request.POST['andor1'])
except (KeyError, ValueError):
    andor1 = 0

Now it's an integer and your checks below (if andor1 == 1) should succeed.

Alternatively test for strings:

if andor1 == "1":
    ...
Ferdinand Beyer
A: 

thank you! The quotes worked. My thinking was stuck on the fact that the value was a numeric and shouldn't need quotes, so I didn't try that.