38)django-组合搜索
一:组合搜索
组合搜索可以用来实现快速查询。效果图举例。瓜子网站选车
注意:URL中的地址0-0什么的是传递的参数的值。
二:实现组合搜索
组合实现条件
1)有外键或者多对多多关系
2)有choice选项
组合实现原理
1)利用上次访问的URL中参数的值,来等记传递的参数
2)全部数据参数为0,其他参数为数据的id
3)第一个默认参数都为0,即是全部数据
三:示例
model.py
from django.db import models # Create your models here. class Category(models.Model): caption = models.CharField(max_length=16) # class ArticleType(models.Model): # caption = models.CharField(max_length=16) class Article(models.Model): title = models.CharField(max_length=32) content = models.CharField(max_length=255) category = models.ForeignKey(Category) # article_type = models.ForeignKey(ArticleType) type_choice = ( (1,'Python'), (2,'OpenStack'), (3,'Linux'), ) article_type_id = models.IntegerField(choices=type_choice)
views.py
from django.shortcuts import render from app01 import models def article(request,*args,**kwargs): print(kwargs) # print(request.path_info) # 获取当前URL # from django.urls import reverse # # {'article_type_id': '0', 'category_id': '0'} # url = reverse('article',kwargs={'article_type_id': '1', 'category_id': '0'}) # print(url) # print(kwargs) # {'article_type_id': '0', 'category_id': '0'} condition = {} for k,v in kwargs.items(): kwargs[k] = int(v) if v == '0': pass else: condition[k] = v # article_type_list = models.ArticleType.objects.all() article_type_list = models.Article.type_choice category_list = models.Category.objects.all() result = models.Article.objects.filter(**condition) return render( request, 'article.html', { 'result': result, 'article_type_list': article_type_list, 'category_list': category_list, 'arg_dict': kwargs } )
模板
{% load filter %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> .condition a{ display: inline-block; padding: 3px 5px; border: 1px solid #dddddd; margin: 5px ; } .condition a.active{ background-color: brown; } </style> </head> <body> <h1>过滤条件</h1> <div class="condition"> <div> {% filter_all arg_dict 'article_type_id' %} {% filter_article_type article_type_list arg_dict %} </div> <div> {% filter_all arg_dict 'category_id' %} {% for row in category_list %} {% if row.id == arg_dict.category_id %} <a class="active" href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a> {% else %} <a href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a> {% endif %} {% endfor %} </div> </div> <h1>查询结果</h1> <ul> {% for row in result %} <li>{{ row.id }}-{{ row.title }}</li> {% endfor %} </ul> </body> </html>
自定义的过滤器
from django import template from django.utils.safestring import mark_safe register = template.Library() @register.simple_tag def filter_all(arg_dict,k): """ {% if arg_dict.article_type_id == 0 %} <a class="active" href="/article-0-{{ arg_dict.category_id }}.html">全部</a> {% else %} <a href="/article-0-{{ arg_dict.category_id }}.html">全部</a> {% endif %} :return: """ if k == 'article_type_id': n1 = arg_dict['article_type_id'] n2 = arg_dict['category_id'] if n1 == 0: ret = '<a class="active" href="/article-0-%s.html">全部</a>' % n2 else: ret = '<a href="/article-0-%s.html">全部</a>' % n2 else: n1 = arg_dict['category_id'] n2 = arg_dict['article_type_id'] if n1 == 0: ret = '<a class="active" href="/article-%s-0.html">全部</a>' % n2 else: ret = '<a href="/article-%s-0.html">全部</a>' % n2 return mark_safe(ret) @register.simple_tag def filter_article_type(article_type_list,arg_dict): """ {% for row in article_type_list %} {% if row.id == arg_dict.article_type_id %} {% else %} <a href="/article-{{ row.id }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a> {% endif %} {% endfor %} :return: """ ret = [] for row in article_type_list: if row[0] == arg_dict['article_type_id']: temp = '<a class="active" href="/article-%s-%s.html">%s</a>' %(row[0],arg_dict['category_id'],row[1],) else: temp = '<a href="/article-%s-%s.html">%s</a>' %(row[0],arg_dict['category_id'],row[1],) ret.append(temp) return mark_safe(''.join(ret))