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='http://www.wxshucaidpc.com/bcjs/?type=' rel='external nofollow' role='button'>全部</a> <a href='http://www.wxshucaidpc.com/bcjs/?type=fl' rel='external nofollow' role='button'>法律</a> <a href='http://www.wxshucaidpc.com/bcjs/?type=xzfg' rel='external nofollow' role='button'>行政法規</a> <a href='http://www.wxshucaidpc.com/bcjs/?type=bmgz' rel='external nofollow' role='button'>部門規章</a> <a href='http://www.wxshucaidpc.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列表篩選功能的實現代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
