首页 > 编程 > 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>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表