1.純模板語言實現
2.自定義simpletag實現(本質是簡化了純模板語言的判斷)
原理都是通過django路由系統,匹配url篩選條件,將篩選條件作為數據庫查詢結果,返回給前端。
例如:路由系統中的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},然后將該條件作為數據庫查詢條件,最后得出結果返回給前端
urls.py
#!/usr/bin/env python3#_*_ coding:utf-8 _*_#Author:wdfrom django.conf.urls import urlfrom . import viewsurlpatterns = [ 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 modelsclass 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>
新聞熱點
疑難解答