
views,中设置请求的类型
class LawDetailView(View):
def get(self, request, law_id):
type = request.GET.get('type', '')
law = Law.objects.get(id=law_id)
return render(request, 'zcfg-detail.html', {
'law': law,
'type': type,
})
templates,中设置:
全部
法律
行政法规
部门规章
地方规章
补充知识:django 一种动态查询的便捷实现过程
问题引出
你可能遇到这种情况,在前端页面上有查询功能,要查询的输入选择有A,B,C等,可以通过任意一个查询,或者任意组合进行查询。
在后端,你可以使用request.GET['A']获取传入的数值。
我们需要判断哪个有输入,再在数据库中进行查询,这样比较麻烦。
解决方案
动态实现查询过程
kwargs = {}
if A is not None:
kwargs['name__startWith'] = A
if B is not None:
kwargs['address__contains'] = B
if C is not None:
kwargs['mobile__endWith'] = C
...
...
personList = Person.objects.filter(**kwargs)
...
注:
A B C 等,为前端传输过来的数据
name address mobile 等,需为你要查询的表的属性字段
startWith contains endWith 等,为你要筛选的规则
Person 为model 表名
以上这篇django列表筛选功能的实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)