Django實現(xiàn)組合搜索的方法示例
一、實現(xiàn)方法
1.純模板語言實現(xiàn)
2.自定義simpletag實現(xiàn)(本質是簡化了純模板語言的判斷)
二、基本原理
原理都是通過django路由系統(tǒng),匹配url篩選條件,將篩選條件作為數據庫查詢結果,返回給前端。
例如:路由系統(tǒng)中的url格式是這樣:
url(r'^article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html',views.filter)
其中article_type_id和category_id和數據庫中字段是相對應的,此時當一個url為article-1-2.html時候,后臺處理函數的參數將是一個字典{'article_type_id': 1, 'category_id': 1},然后將該條件作為數據庫查詢條件,最后得出結果返回給前端
三、代碼樣例
方法1:純模板語言實現(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是數字,所以這里需要轉換
if v=="0":#當條件為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,#將當前的篩選條件傳遞給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>查詢結果</h1>
<div>
{% for row in aritcle %}
<div>{{ row.id }}-{{ row.title }}</div>
{% endfor %}
</div>
</body>
</html>
方法二:使用simpletag實現(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):
'''
處理條件為全部
:param article_arg: 當前url字典:如{'article_type_id': 1, 'category_id': 1}
:param condition: 要處理的條件,如article_type_id,用于區(qū)分當前處理選擇了那個全部
: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對象
:param article_arg: 當前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對象
:param article_arg: 當前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>查詢結果</h1>
<div>
{% for row in aritcle %}
<div>{{ row.id }}-{{ row.title }}</div>
{% endfor %}
</div>
</body>
</html>
ps附上簡圖:

四、其他變化
在如上的示例中,我們的過濾條件是從數據庫中拿到,有時候我們定義的時候使用的是靜態(tài)字段,此時組合搜索會稍微修改。
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)字段放入內存
(1,'linux'),
(2,'python'),
(3,'go'),
)
2.處理函數變化
###獲取#### aritcle_type=models.Article.article_type#直接獲取類的靜態(tài)字段
3.simpletag相應改變
###由于我們傳遞的元祖,所以取值使用元祖方式 article_type[0]# 篩選條件id article_type[1]# 篩選條件名稱
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python Pandas describe()函數的使用詳解
pandas庫中的describe()函數為我們提供了這樣的功能,它可以快速生成數據集的描述性統(tǒng)計信息,這篇文章主要介紹了Python Pandas describe()函數的使用介紹,需要的朋友可以參考下2024-05-05
python裝飾器實現(xiàn)對異常代碼出現(xiàn)進行自動監(jiān)控的實現(xiàn)方法
這篇文章主要介紹了python裝飾器實現(xiàn)對異常代碼出現(xiàn)進行自動監(jiān)控的實現(xiàn)方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Python執(zhí)行系統(tǒng)命令的五種方式小結
在日常開發(fā)中,有時需要在Python腳本中執(zhí)行系統(tǒng)命令,Python有五種方式來執(zhí)行系統(tǒng)命令(推薦使用第五種),本文為大家整理了這五種方法的具體使用,希望對大家有所幫助2024-01-01
解決PIP安裝第三方庫報錯SSL: CERTIFICATE_VERIFY_FAILED問題
這篇文章主要介紹了解決PIP安裝第三方庫報錯SSL: CERTIFICATE_VERIFY_FAILED問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
python使用pandas讀xlsx文件的實現(xiàn)
這篇文章主要介紹了python使用pandas讀xlsx文件的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05

