国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Django實現組合搜索的方法示例

2020-01-04 16:04:03
字體:
來源:轉載
供稿:網友

一、實現方法

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},然后將該條件作為數據庫查詢條件,最后得出結果返回給前端

三、代碼樣例

方法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):  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實現

定義simpletag

myfilter.py

#!/usr/bin/env python3#_*_ coding:utf-8 _*_#Author:wdfrom django import templatefrom django.utils.safestring import mark_saferegister=template.Library()@register.simple_tagdef filter_all(article_arg,condition):  '''  處理條件為全部  :param article_arg: 當前url字典:如{'article_type_id': 1, 'category_id': 1}  :param condition: 要處理的條件,如article_type_id,用于區分當前處理選擇了那個全部  :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_tagdef 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_tagdef 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附上簡圖:

Django,組合搜索,Django實現組合搜索

四、其他變化

在如上的示例中,我們的過濾條件是從數據庫中拿到,有時候我們定義的時候使用的是靜態字段,此時組合搜索會稍微修改。

1.model定義

class Article(models.Model):  content=models.CharField(max_length=256)  category=models.ForeignKey(to='Category')  article_type=(  #使用靜態字段放入內存    (1,'linux'),    (2,'python'),    (3,'go'),  )

2.處理函數變化

###獲取####aritcle_type=models.Article.article_type#直接獲取類的靜態字段

3.simpletag相應改變

###由于我們傳遞的元祖,所以取值使用元祖方式article_type[0]# 篩選條件idarticle_type[1]# 篩選條件名稱

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扬州市| 威宁| 双柏县| 宝山区| 黎平县| 鄂托克前旗| 普洱| 定日县| 马公市| 江口县| 宜君县| 偏关县| 湖州市| 马龙县| 醴陵市| 广饶县| 滨海县| 犍为县| 武汉市| 中宁县| 蒙阴县| 云南省| 新疆| 金平| 抚松县| 三门峡市| 辰溪县| 宁津县| 永吉县| 保定市| 韶关市| 密云县| 淮安市| 凉城县| 阿合奇县| 寿光市| 北安市| 金山区| 高邑县| 东丽区| 昆明市|