我正在构建一个表单,用户可以在其中选择几个选项,并通过一个查询来过滤结果。例如,用户可以在初始形式上选择一个或多个商业垂直,如零售、餐饮等,在第二个形式上可以选择云、本地等软件实现。。。以及更多其他字段来构造查询以筛选结果。表单的每个部分(行业、部署模型)上的值可能有值,也可能没有值
我让过滤器部分工作,但有些错误。例如,如果我提交的表单的“行业”部分的值是在返回的表单结果上设置的初始复选框是
[<django.db.models.query_utils.Q object at 0x7f6b60b854d0>]
但如果我只在表单的最后一部分提交值,它就会正常工作并按预期返回结果。在
^{pr2}$<form action="" method="get" id="filterform"><div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='adsfasf' /></div>
<p><label for="id_industry_0">Industry:</label> <ul>
<li><label for="id_industry_0"><input type="checkbox" name="industry" value="8" id="id_industry_0" /> clothing</label></li>
<li><label for="id_industry_1"><input type="checkbox" name="industry" value="7" id="id_industry_1" /> food</label></li>
<li><label for="id_industry_2"><input type="checkbox" name="industry" value="5" id="id_industry_2" /> Apparel</label></li>
<li><label for="id_industry_3"><input type="checkbox" name="industry" value="9" id="id_industry_3" /> food truck</label></li>
<li><label for="id_industry_4"><input type="checkbox" name="industry" value="6" id="id_industry_4" /> Bar/Night Club</label></li>
</ul></p>
<p><label for="id_deployment_model_0">Deployment model:</label> <ul>
<li><label for="id_deployment_model_0"><input type="checkbox" name="deployment_model" value="4" id="id_deployment_model_0" /> On Premise</label></li>
<li><label for="id_deployment_model_1"><input type="checkbox" name="deployment_model" value="6" id="id_deployment_model_1" /> Software as a Service</label></li>
<li><label for="id_deployment_model_2"><input type="checkbox" name="deployment_model" value="8" id="id_deployment_model_2" /> server</label></li>
<li><label for="id_deployment_model_3"><input type="checkbox" name="deployment_model" value="5" id="id_deployment_model_3" /> Cloud</label></li>
<li><label for="id_deployment_model_4"><input type="checkbox" name="deployment_model" value="7" id="id_deployment_model_4" /> premise</label></li>
</ul></p>
<input type="submit" value="Submit" />
</form>
您的问题似乎是结果可以包含两个可能的值。 Q对象或查询集的列表。 在
software_list
视图中,使用queryset覆盖结果,但仅当设置了部署模型参数时。result = Software.objects.filter(reduce(operator.and_, result))
缩进一次到多个。当只供应工业的时候就跳过了。在例如:
应该是:
^{pr2}$整个方法如下:
if语句中还有一个小缺陷:
^{4}$if 'industry' in request.GET and 'industry' is not None:
,因为字符串“industry”永远不会是None。你可能是想用:或简化(感谢乔·霍洛威):
相关问题 更多 >
编程相关推荐