Django實(shí)現(xiàn)組合搜索的方法示例
一、實(shí)現(xiàn)方法
1.純模板語言實(shí)現(xiàn)
2.自定義simpletag實(shí)現(xiàn)(本質(zhì)是簡(jiǎn)化了純模板語言的判斷)
二、基本原理
原理都是通過django路由系統(tǒng),匹配url篩選條件,將篩選條件作為數(shù)據(jù)庫(kù)查詢結(jié)果,返回給前端。
例如:路由系統(tǒng)中的url格式是這樣:
url(r'^article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html',views.filter)
其中article_type_id和category_id和數(shù)據(jù)庫(kù)中字段是相對(duì)應(yīng)的,此時(shí)當(dāng)一個(gè)url為article-1-2.html時(shí)候,后臺(tái)處理函數(shù)的參數(shù)將是一個(gè)字典{'article_type_id': 1, 'category_id': 1},然后將該條件作為數(shù)據(jù)庫(kù)查詢條件,最后得出結(jié)果返回給前端
三、代碼樣例
方法1:純模板語言實(shí)現(xiàn)
urls.py
#!/usr/bin/env python3 #_*_ coding:utf-8 _*_ #Author:wd from django.conf.urls import url from . import views urlpatterns = [ url(r'^$',views.index), url(r'^article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html',views.filter), ]
models.py
from django.db import models class Category(models.Model): caption=models.CharField(max_length=64) class Article_type(models.Model): caption=models.CharField(max_length=64) class Article(models.Model): title=models.CharField(max_length=64) content=models.CharField(max_length=256) category=models.ForeignKey(to='Category') article_type=models.ForeignKey(to='Article_type'
views.py
def filter(request,*args,**kwargs):
if request.method=="GET":
condition={}
for k,v in kwargs.items():
kwargs[k]=int(v) #模板if判斷row.id是數(shù)字,所以這里需要轉(zhuǎn)換
if v=="0":#當(dāng)條件為0代表所選的是全部,那么就不必要加入到過濾條件中
pass
else:
condition[k]=int(v)
aritcle=models.Article.objects.filter(**condition)
aritcle_type=models.Article_type.objects.all()
aritcle_category=models.Category.objects.all()
return render(request,'search.html',{
'aritcle':aritcle,
'article_type':aritcle_type,
'article_category':aritcle_category,
'article_arg':kwargs,#將當(dāng)前的篩選條件傳遞給html
})
html模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.container a{
display: inline-block;
padding: 3px 5px;
margin: 5px;
border: 1px solid #dddddd ;
}
.active{
background-color: rebeccapurple;
}
</style>
</head>
<body>
<h1>搜索條件</h1>
<div class="container">
{% if article_arg.article_type_id == 0 %}
<a class="active" href="/cmdb/article-0-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >全部</a>
{% else %}
<a href="/cmdb/article-0-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >全部</a>
{% endif %}
{% for row in article_type %}
{% if row.id == article_arg.article_type_id %}
<a class="active" href="/cmdb/article-{{ row.id }}-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% else %}
<a href="/cmdb/article-{{ row.id }}-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% endif %}
{% endfor %}
</div>
<div class="container">
{% if article_arg.category_id == 0 %}
<a class="active" href="/cmdb/article-{{ article_arg.article_type_id }}-0.html" rel="external nofollow" rel="external nofollow" >全部</a>
{% else %}
<a href="/cmdb/article-{{ article_arg.article_type_id }}-0.html" rel="external nofollow" rel="external nofollow" >全部</a>
{% endif %}
{% for row in article_category %}
{% if row.id == article_arg.category_id %}
<a class="active" href="/cmdb/article-{{ article_arg.article_type_id }}-{{ row.id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% else %}
<a href="/cmdb/article-{{ article_arg.article_type_id }}-{{ row.id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% endif %}
{% endfor %}
</div>
<h1>查詢結(jié)果</h1>
<div>
{% for row in aritcle %}
<div>{{ row.id }}-{{ row.title }}</div>
{% endfor %}
</div>
</body>
</html>
方法二:使用simpletag實(shí)現(xiàn)
定義simpletag
myfilter.py
#!/usr/bin/env python3
#_*_ coding:utf-8 _*_
#Author:wd
from django import template
from django.utils.safestring import mark_safe
register=template.Library()
@register.simple_tag
def filter_all(article_arg,condition):
'''
處理?xiàng)l件為全部
:param article_arg: 當(dāng)前url字典:如{'article_type_id': 1, 'category_id': 1}
:param condition: 要處理的條件,如article_type_id,用于區(qū)分當(dāng)前處理選擇了那個(gè)全部
:return: 返回下面頁面形式
{% if article_arg.article_type_id == 0 %}
<a class="active" href="/cmdb/article-0-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >全部</a>
{% else %}
<a href="/cmdb/article-0-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >全部</a>
{% endif %}
{% for row in article_type %}
{% if row.id == article_arg.article_type_id %}
<a class="active" href="/cmdb/article-{{ row.id }}-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% else %}
<a href="/cmdb/article-{{ row.id }}-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% endif %}
{% endfor %}
'''
if condition=='article_type_id':
if article_arg[condition]==0:
print(article_arg['category_id'])
res= '<a class ="active" href="/cmdb/article-0-%s.html" rel="external nofollow" rel="external nofollow" >全部</a>' % article_arg['category_id']
else:
res = '<a href="/cmdb/article-0-%s.html" rel="external nofollow" rel="external nofollow" >全部</a>' % article_arg['category_id']
return mark_safe(res)
elif condition=='category_id':
if article_arg['category_id']==0:
res = '<a class ="active" href="/cmdb/article-%s-0.html" rel="external nofollow" rel="external nofollow" >全部</a>' % article_arg['article_type_id']
else:
res = '<a href="/cmdb/article-%s-0.html" rel="external nofollow" rel="external nofollow" >全部</a>' % article_arg['article_type_id']
return mark_safe(res)
@register.simple_tag
def filter_type(article_type,article_arg):
'''
:param article_type: article_type對(duì)象
:param article_arg: 當(dāng)前url字典
:return:
{% for row in article_type %}
{% if row.id == article_arg.article_type_id %}
<a class="active" href="/cmdb/article-{{ row.id }}-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% else %}
<a href="/cmdb/article-{{ row.id }}-{{ article_arg.category_id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% endif %}
{% endfor %}
'''
res=[]
for row in article_type:
if row.id== article_arg['article_type_id']:
temp='<a class="active" href="/cmdb/article-%s-%s.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a>' %(row.id,article_arg['category_id'],row.caption)
else:
temp = '<a href="/cmdb/article-%s-%s.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a>' % (row.id, article_arg['category_id'],row.caption)
res.append(temp)
return mark_safe("".join(res))
@register.simple_tag
def filter_category(article_category,article_arg):
'''
:param article_type: article_category對(duì)象
:param article_arg: 當(dāng)前url字典
:return:
{% for row in article_category %}
{% if row.id == article_arg.category_id %}
<a class="active" href="/cmdb/article-{{ article_arg.article_type_id }}-{{ row.id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% else %}
<a href="/cmdb/article-{{ article_arg.article_type_id }}-{{ row.id }}.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ row.caption }}</a>
{% endif %}
{% endfor %}
'''
res=[]
for row in article_category:
if row.id== article_arg['category_id']:
temp='<a class="active" href="/cmdb/article-%s-%s.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a>' %(article_arg['article_type_id'],row.id,row.caption)
else:
temp = '<a href="/cmdb/article-%s-%s.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a>' % (article_arg['article_type_id'],row.id,row.caption)
res.append(temp)
return mark_safe("".join(res))
html模板
{% load myfilter %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.container a{
display: inline-block;
padding: 3px 5px;
margin: 5px;
border: 1px solid #dddddd ;
}
.active{
background-color: rebeccapurple;
}
</style>
</head>
<body>
<h1>搜索條件</h1>
<div class="container">
{% filter_all article_arg 'article_type_id' %}
{% filter_type article_type article_arg %}
</div>
<div class="container">
{% filter_all article_arg 'category_id' %}
{% filter_category article_category article_arg %}
</div>
<h1>查詢結(jié)果</h1>
<div>
{% for row in aritcle %}
<div>{{ row.id }}-{{ row.title }}</div>
{% endfor %}
</div>
</body>
</html>
ps附上簡(jiǎn)圖:

四、其他變化
在如上的示例中,我們的過濾條件是從數(shù)據(jù)庫(kù)中拿到,有時(shí)候我們定義的時(shí)候使用的是靜態(tài)字段,此時(shí)組合搜索會(huì)稍微修改。
1.model定義
class Article(models.Model):
title=models.CharField(max_length=64)
content=models.CharField(max_length=256)
category=models.ForeignKey(to='Category')
article_type=( #使用靜態(tài)字段放入內(nèi)存
(1,'linux'),
(2,'python'),
(3,'go'),
)
2.處理函數(shù)變化
###獲取#### aritcle_type=models.Article.article_type#直接獲取類的靜態(tài)字段
3.simpletag相應(yīng)改變
###由于我們傳遞的元祖,所以取值使用元祖方式 article_type[0]# 篩選條件id article_type[1]# 篩選條件名稱
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)戰(zhàn)之異步獲取中國(guó)天氣信息
這篇文章主要介紹了如何利用Python爬蟲異步獲取天氣信息,用的API是中國(guó)天氣網(wǎng)。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試2022-03-03
Python Pandas describe()函數(shù)的使用詳解
pandas庫(kù)中的describe()函數(shù)為我們提供了這樣的功能,它可以快速生成數(shù)據(jù)集的描述性統(tǒng)計(jì)信息,這篇文章主要介紹了Python Pandas describe()函數(shù)的使用介紹,需要的朋友可以參考下2024-05-05
python裝飾器實(shí)現(xiàn)對(duì)異常代碼出現(xiàn)進(jìn)行自動(dòng)監(jiān)控的實(shí)現(xiàn)方法
這篇文章主要介紹了python裝飾器實(shí)現(xiàn)對(duì)異常代碼出現(xiàn)進(jìn)行自動(dòng)監(jiān)控的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
淺析如何在Python中使用結(jié)構(gòu)模式匹配
在Python 3.10中引入了模式匹配語法,允許我們?cè)趹?yīng)用程序中使用強(qiáng)大的新編程技術(shù)進(jìn)行決策,下面我們就來講講如何在Python中使用結(jié)構(gòu)模式匹配吧2023-08-08
Python執(zhí)行系統(tǒng)命令的五種方式小結(jié)
在日常開發(fā)中,有時(shí)需要在Python腳本中執(zhí)行系統(tǒng)命令,Python有五種方式來執(zhí)行系統(tǒng)命令(推薦使用第五種),本文為大家整理了這五種方法的具體使用,希望對(duì)大家有所幫助2024-01-01
解決PIP安裝第三方庫(kù)報(bào)錯(cuò)SSL: CERTIFICATE_VERIFY_FAILED問題
這篇文章主要介紹了解決PIP安裝第三方庫(kù)報(bào)錯(cuò)SSL: CERTIFICATE_VERIFY_FAILED問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
python使用pandas讀xlsx文件的實(shí)現(xiàn)
這篇文章主要介紹了python使用pandas讀xlsx文件的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

