国产探花免费观看_亚洲丰满少妇自慰呻吟_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>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乾安县| 兖州市| 如东县| 句容市| 工布江达县| 靖边县| 顺义区| 本溪市| 靖宇县| 沁阳市| 泌阳县| 嘉鱼县| 枣强县| 古田县| 三台县| 光泽县| 孝义市| 红原县| 郯城县| 蒲江县| 宝坻区| 和顺县| 连云港市| 当阳市| 马尔康县| 富锦市| 大冶市| 永兴县| 五莲县| 彰化市| 蓝田县| 丽水市| 天镇县| 马龙县| 宜春市| 张北县| 乌拉特前旗| 濉溪县| 古交市| 白水县| 全州县|