django列表篩選功能的實(shí)現(xiàn)代碼
views,中設(shè)置請(qǐng)求的類型
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,中設(shè)置:
<div style='margin-bottom: 20px;'> <a href='http://www.piao2010.com/bcjs/?type=' rel='external nofollow' role='button'>全部</a> <a href='http://www.piao2010.com/bcjs/?type=fl' rel='external nofollow' role='button'>法律</a> <a href='http://www.piao2010.com/bcjs/?type=xzfg' rel='external nofollow' role='button'>行政法規(guī)</a> <a href='http://www.piao2010.com/bcjs/?type=bmgz' rel='external nofollow' role='button'>部門規(guī)章</a> <a href='http://www.piao2010.com/bcjs/?type=dfgz' rel='external nofollow' role='button'>地方規(guī)章</a></div>
補(bǔ)充知識(shí):django 一種動(dòng)態(tài)查詢的便捷實(shí)現(xiàn)過(guò)程
問(wèn)題引出
你可能遇到這種情況,在前端頁(yè)面上有查詢功能,要查詢的輸入選擇有A,B,C等,可以通過(guò)任意一個(gè)查詢,或者任意組合進(jìn)行查詢。
在后端,你可以使用request.GET[’A’]獲取傳入的數(shù)值。
我們需要判斷哪個(gè)有輸入,再在數(shù)據(jù)庫(kù)中進(jìn)行查詢,這樣比較麻煩。
解決方案
動(dòng)態(tài)實(shí)現(xiàn)查詢過(guò)程
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 等,為前端傳輸過(guò)來(lái)的數(shù)據(jù)
name address mobile 等,需為你要查詢的表的屬性字段
startWith contains endWith 等,為你要篩選的規(guī)則
Person 為model 表名
以上這篇django列表篩選功能的實(shí)現(xiàn)代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP.Net MVC利用NPOI導(dǎo)入導(dǎo)出Excel的示例代碼2. adodb.recordset.open(rs.open)方法參數(shù)詳解3. asp文件如何打開(kāi)4. 怎樣打開(kāi)XML文件?xml文件如何打開(kāi)?5. ASP和PHP文件操作速度的對(duì)比6. ASP替換、保存遠(yuǎn)程圖片實(shí)現(xiàn)代碼7. ASP中Server.HTMLEncode用法(附自定義函數(shù))8. Spring依賴注入的三種方式實(shí)例詳解9. WML教程之文本框控件Input10. ASP基礎(chǔ)入門第二篇(ASP基礎(chǔ)知識(shí))
