成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

django列表篩選功能的實(shí)現(xiàn)代碼

瀏覽:135日期:2024-10-19 08:20:23

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)。

標(biāo)簽: Django
相關(guān)文章: