django列表筛选功能的实现代码
作者:sinat_14977477 时间:2021-06-23 08:48:57
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 class="col-lg-12" style="margin-bottom: 20px;">
<a class="{% if type == '' %}btn btn-danger{% else %}btn btn-default{% endif %}" href="?type=" rel="external nofollow" role="button">全部</a>
<a class="{% if type == 'fl' %}btn btn-danger{% else %}btn btn-default{% endif %}" href="?type=fl" rel="external nofollow" role="button">法律</a>
<a class="{% if type == 'xzfg' %}btn btn-danger{% else %}btn btn-default{% endif %}" href="?type=xzfg" rel="external nofollow" role="button">行政法规</a>
<a class="{% if type == 'bmgz' %}btn btn-danger{% else %}btn btn-default{% endif %}" href="?type=bmgz" rel="external nofollow" role="button">部门规章</a>
<a class="{% if type == 'dfgz' %}btn btn-danger{% else %}btn btn-default{% endif %}" href="?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'] = 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 表名
来源:https://blog.csdn.net/sinat_14977477/article/details/81436015
标签:django,列表,筛选
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
网站程序员如何应对web标准
2007-05-11 16:52:00
![](https://img.aspxhome.com/file/uploadpic/20075/2007511165222957.gif)
一文带你搞懂Python上下文管理器
2022-11-26 05:15:50
如何使用Python 抓取和优化所有网站图像
2022-05-18 12:36:00
![](https://img.aspxhome.com/file/2023/0/77660_0s.png)
css去掉checkbox边框的方法
2011-06-06 10:32:00
ajax代理程序,自动判断字符编码
2007-11-04 13:17:00
详解pandas绘制矩阵散点图(scatter_matrix)的方法
2022-06-09 18:52:34
![](https://img.aspxhome.com/file/2023/7/107207_0s.png)
批处理与python代码混合编程的方法
2023-08-26 10:45:32
Python+OpenCV让电脑帮你玩微信跳一跳
2021-12-16 10:31:17
![](https://img.aspxhome.com/file/2023/2/66812_0s.jpg)
OpenCV实现去除背景识别的方法总结
2021-01-06 23:04:10
![](https://img.aspxhome.com/file/2023/0/83880_0s.jpg)
他们是如何不让我的Teleport和Webzip工作的?
2010-07-14 21:06:00
ASP模拟POST提交数据的方法
2010-01-14 20:06:00
windows下python 3.6.4安装配置图文教程
2023-08-10 02:57:37
![](https://img.aspxhome.com/file/2023/7/77827_0s.jpg)
HTML5 离线存储之Web SQL
2011-06-19 14:13:19
系统默认的MySQL用户名消失的解决方法
2008-12-02 14:26:00
python使用re模块爬取豆瓣Top250电影
2023-10-11 17:28:38
![](https://img.aspxhome.com/file/2023/0/82650_0s.png)
go语言LeetCode题解720词典中最长的单词
2023-08-05 19:46:04
PHP set_time_limit(0)长连接的实现分析
2023-11-06 11:46:20
关于Django框架的关系模型序列化和一对多关系中的序列化解析
2023-01-15 00:52:53
![](https://img.aspxhome.com/file/2023/1/79531_0s.jpg)
Python大数据之从网页上爬取数据的方法详解
2023-10-06 22:10:53
![](https://img.aspxhome.com/file/2023/3/72023_0s.png)
phpstudy apache开启ssi使用详解
2023-05-25 08:04:44