0

模板:

<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>

实际上有三个搜索选择(病理、商品、技术)用户可以做和/或混合或匹配这三个,这就是为什么我需要在 views.py 中的和/或选项。

意见.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)
    )

我知道我的所有代码都可以正常工作,但是 (if andor1 ==1:) 和 (if andor1 ==2:) 行被忽略了。我怀疑 andor1 的值没有出现在我使用它的地方。我认为它直到返回 render_to_response 之后才真正呈现,因为它作为一个值出现在下一个模板中,否则我会在模板中的 if andor1 ==1: 处看到某种响应。有什么建议么?

4

3 回答 3

1

的值andor1是从 HTML 表单传递的字符串,"1" == 1在 Python 中为 False。尝试以下操作:

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

现在它是一个整数,您在 ( if andor1 == 1) 下面的检查应该会成功。

或者测试字符串:

if andor1 == "1":
    ...
于 2009-03-09T16:53:02.210 回答
0

dpaste 代码: http ://dpaste.com/10899/

于 2009-03-09T16:39:06.520 回答
0

谢谢你!报价有效。我的想法停留在该值是数字并且不需要引号的事实上,所以我没有尝试。

于 2009-03-09T17:02:27.180 回答