django列表筛选功能的实现代码
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,中设置:
<div style='margin-bottom: 20px;'> <a href='https://www.haobala.com/bcjs/?type=' rel='external nofollow' role='button'>全部</a> <a href='https://www.haobala.com/bcjs/?type=fl' rel='external nofollow' role='button'>法律</a> <a href='https://www.haobala.com/bcjs/?type=xzfg' rel='external nofollow' role='button'>行政法规</a> <a href='https://www.haobala.com/bcjs/?type=bmgz' rel='external nofollow' role='button'>部门规章</a> <a href='https://www.haobala.com/bcjs/?type=dfgz' rel='external nofollow' role='button'>地方规章</a></div>
补充知识:django 一种动态查询的便捷实现过程
问题引出
你可能遇到这种情况,在前端页面上有查询功能,要查询的输入选择有A,B,C等,可以通过任意一个查询,或者任意组合进行查询。
在后端,你可以使用request.GET[’A’]获取传入的数值。
我们需要判断哪个有输入,再在数据库中进行查询,这样比较麻烦。
解决方案
动态实现查询过程
kwargs = {}if A is not None: kwargs[’name__startWith’] = Aif B is not None: kwargs[’address__contains’] = Bif 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列表筛选功能的实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持好吧啦网。
相关文章:
1. PHP设计模式之迭代器模式浅析2. JXTA Java标准版本 2.3.7 发布3. JS实现手写 forEach算法示例4. Vue axios与Go Frame后端框架的Options请求跨域问题详解5. ASP.NET MVC使用正则表达式验证手机号码6. Pycharm中安装wordcloud等库失败问题及终端通过pip安装的Python库如何添加到Pycharm解释器中(推荐)7. 用css截取字符的几种方法详解(css排版隐藏溢出文本)8. python b站视频下载的五种版本9. python生成xml时规定dtd实例方法10. Android Studio3.6新特性之视图绑定ViewBinding使用指南

网公网安备