一、实现方法

1.纯模板语言实现

2.自定义simpletag实现(本质是简化了纯模板语言的判断)

 

二、基本原理

原理都是通过django路由系统,匹配url筛选条件,将筛选条件作为数据库查询结果,返回给前端。

例如:路由系统中的url格式是这样:

1
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

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python3
#_*_ coding:utf-8 _*_
#Author:wd
from django.conf.urls import url
 
from . import views
urlpatterns = [
  url(r'^$',views.index),
  url(r'^article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html',views.filter),
]

models.py

1
2
3
4
5
6
7
8
9
10
11
12
13
from django.db import models
 
class 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!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{
      
 
    }
  </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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
#_*_ coding:utf-8 _*_
#Author:wd
from django import template
from django.utils.safestring import mark_safe
 
register=template.Library()
@register.simple_tag
def 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_tag
def 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_tag
def 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模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{% 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{
      
 
    }
  </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附上简图:

 

四、其他变化

在如上的示例中,我们的过滤条件是从数据库中拿到,有时候我们定义的时候使用的是静态字段,此时组合搜索会稍微修改。

 

1.model定义

1
2
3
4
5
6
7
8
9
class Article(models.Model):
  title=models.CharField(max_length=64)
  content=models.CharField(max_length=256)
  category=models.ForeignKey(to='Category')
  article_type=#使用静态字段放入内存
    (1,'linux'),
    (2,'python'),
    (3,'go'),
  )
 

2.处理函数变化

1
2
###获取####
aritcle_type=models.Article.article_type#直接获取类的静态字段
 

3.simpletag相应改变

1
2
3
###由于我们传递的元祖,所以取值使用元祖方式
article_type[0]# 筛选条件id
article_type[1]# 筛选条件名称

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

原文链接:http://www.cnblogs.com/wdliu/p/8331375.html

Django实现组合搜索的方法示例的更多相关文章

  1. Django之组合搜索组件(二)--另附simple_tag的创建使用方法

    这次的代码为Django之组合搜索组件(一)的改版,实现的结果和(一)相同,不同的是,这次运用simple_tag方式,使.html程序简化 所以现在就开始编程吧! 首先想使用simple_tag方法 ...

  2. Django实现组合搜索

    一.实现方法 1.纯模板语言实现 2.自定义simpletag实现(本质是简化了纯模板语言的判断) 二.基本原理 原理都是通过django路由系统,匹配url筛选条件,将筛选条件作为数据库查询结果,返 ...

  3. python(Django之组合搜索、JSONP、XSS过滤 )

    一.组合搜索 二.jsonp 三.xss过滤 一.组合搜索 首先,我们在做一个门户网站的时候,前端肯定是要进行搜索的,但是如果搜索的类型比较多的话,怎么做才能一目了然的,这样就引出了组合搜索的这个案例 ...

  4. Django中组合搜索功能

    需求分析 很多电商网站中有组合搜索的功能,所谓组合搜索就是网页中组合多个条件,对数据库中进行查询,并且将结果显示在页面中,看个例子吧: 注意红框中的标识,我们可以根据URL来做组合搜索. video- ...

  5. Django学习---组合搜索组件

    组合搜索组件 我们都会写博客,写文章之后我们要给文章设置目录,设置类型.之后我们在浏览文章的时候就能够按类别进行精确定位到文章,那这个组合搜索我们应该怎么做呢? 首先我们先创建3张表,分别存放文章,文 ...

  6. Django之组合搜索组件(一)

    什么是组合搜索呢? 比如你想买车,但手里只有10万块!所以你只能在10万块的车里挑选,但你喜欢黑色,因为觉得很高端大气上档次,说白了就是装逼杠杠的!之后售车姐给你拿了个表表,你看到了低于10万块且颜色 ...

  7. Django 之组合搜索

    现在很多网站都会有这样的组合搜索功能,其实质是几个模型之间组合对数据库进行查询,并将结果显示到页面上. 每一行都是一个模型,模型之间有着连表关系(一对多.多对多等) 模型设计 总共四个模型:分别为方向 ...

  8. Django实现瀑布流,组合搜索

    Django中组合搜索功能 需求分析 很多电商网站中有组合搜索的功能,所谓组合搜索就是网页中组合多个条件,对数据库中进行查询,并且将结果显示在页面中,看个例子吧: 注意红框中的标识,我们可以根据URL ...

  9. django实现瀑布流、组合搜索、阶梯评论、验证码

    django实现图片瀑布流布局 我们在一些图片网站上经常会看到,满屏都是图片,而且图片都大小不一,却可以按空间排列.默认一个div是占用一行,当想把div里的图片并排显示的时候,只能使用float属性 ...

随机推荐

  1. JAVA中的Session和Cookie【转】

    一.cookie机制和session机制的区别 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案. 同时我们也看到,由于才服务器端保持状态的 ...

  2. AngularJS实现简单的分页功能

    本篇文章由:http://xinpure.com/angularjs-simple-paging-functionality/ 初学 AngularJS, 尝试着写一些小功能 代码逻辑写得略粗糙,仅仅 ...

  3. C# 读 xml注释

    有这么个需求: 要统计所有的配置文件,这些配置文件都xml格式,并把这些配置写到数据表里,如果有注释要把这些注释写到对应配置对象的描述字段上 <item id="" key= ...

  4. 【转】C# 视频监控系列(12):H264播放器——播放录像文件

    原文地址:http://www.cnblogs.com/over140/archive/2009/03/23/1419643.html?spm=5176.100239.blogcont51182.16 ...

  5. Android数据格式解析对象JSON用法

    1.JSON概念: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性,从而可以在不同平台间进行数据交换.JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为. JSON可以将 ...

  6. .Net程序员面试 中级篇 (回答Scott Hanselman的问题)

    继<.Net 程序员面试 C# 语言篇 (回答Scott Hanselman的问题)>跟<.Net程序员面试 每个人都应知道篇 (回答Scott Hanselman的问题)>之 ...

  7. redis安装和配置(一)

    Redis 的官方下载站是http://redis.io/download 怎么安装 Redis 数据库呢?下面将介绍Linux 版本的安装方法 步骤一: 下载Redis 下载安装包:wget htt ...

  8. thinkphp验证码出不来

    import("ORG.Util.Image"); //图像操作类库 ob_end_clean(); $type = isset($_GET['type'])?$_GET['typ ...

  9. JSP中out.write()和out.print()的区别

    out对象的类型是JspWriter.JspWriter继承了java.io.Writer类. 1)print方法是子类JspWriter,write是Writer类中定义的方法: 2)重载的prin ...

  10. ny82 迷宫寻宝(一) map+queue

    题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=82 AC代码:讲解,先统计在可搜索范围内对应的钥匙数,把搜到的门存到另外的一个队列中,第一 ...