模板:
<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: 处看到某种响应。有什么建议么?