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.aoyou183.cn/bcjs/?type=' rel='external nofollow' role='button'>全部</a> <a href='http://www.aoyou183.cn/bcjs/?type=fl' rel='external nofollow' role='button'>法律</a> <a href='http://www.aoyou183.cn/bcjs/?type=xzfg' rel='external nofollow' role='button'>行政法規</a> <a href='http://www.aoyou183.cn/bcjs/?type=bmgz' rel='external nofollow' role='button'>部門規章</a> <a href='http://www.aoyou183.cn/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. ajax請求添加自定義header參數代碼2. php面向對象程序設計介紹3. Python獲取抖音關注列表封號賬號的實現代碼4. Python數據分析之pandas函數詳解5. php測試程序運行速度和頁面執行速度的代碼6. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁7. 三個不常見的 HTML5 實用新特性簡介8. 使用.net core 自帶DI框架實現延遲加載功能9. php網絡安全中命令執行漏洞的產生及本質探究10. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析
