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

首頁 > 編程 > Python > 正文

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

2020-02-22 22:56:55
字體:
來源:轉載
供稿:網友

一、實現方法

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):  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>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桃园县| 陇西县| 石景山区| 乐昌市| 河源市| 稷山县| 卫辉市| 凤阳县| 纳雍县| 海晏县| 南陵县| 利辛县| 出国| 响水县| 冀州市| 昭苏县| 衢州市| 洪泽县| 合山市| 闽侯县| 娱乐| 夏津县| 米脂县| 奉化市| 丹寨县| 崇左市| 张家界市| 宜川县| 玉龙| 玛曲县| 台南市| 鹿泉市| 阜康市| 白银市| 乌什县| 郸城县| 保亭| 余姚市| 唐河县| 会泽县| 涞水县|