组合搜索
做博客后台时,需要根据文章的类型做不同的检索
1、简单实现关联文件 :
from django.conf.urls import url from . import views urlpatterns = [ url(r'^index.html/$',views.index), url(r'^article/(?P<article_type>\d+)-(?P<category>\d+).html/$',views.article) ] url.py <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .condition a{ display:inline-block; padding: 3px 5px; border: 1px solid black; } .condition a.active{ background-color: brown; } </style> </head> <body> <h2>过滤条件</h2> <div class="condition"> {% if kwargs.article_type == 0 %} <a href="/article/0-{{ kwargs.category }}.html" class="active">全部</a> {% else %} <a href="/article/0-{{ kwargs.category }}.html">全部</a> {% endif %} {% for row in article_type %} {% if row.id == kwargs.article_type %} <a class="active" href="/article/{{ row.id }}-{{ kwargs.category }}.html">{{ row.caption }}</a> {% else %} <a href="/article/{{ row.id }}-{{ kwargs.category }}.html">{{ row.caption }}</a> {% endif %} {% endfor %} </div> <div class="condition"> {% if kwargs.category == 0 %} <a class="active" href="/article/{{ kwargs.article_type }}-0.html">全部</a> {% else %} <a href="/article/{{ kwargs.article_type }}-0.html">全部</a> {% endif %} {% for row in category %} {% if row.id == kwargs.category %} <a class="active" href="/article/{{ kwargs.article_type }}-{{ row.id }}.html">{{ row.caption }}</a> {% else %} <a href="/article/{{ kwargs.article_type }}-{{ row.id }}.html">{{ row.caption }}</a> {% endif %} {% endfor %} </div> <h2>查询结果</h2> <ul> {% for row in articles %} <li>{{ row.id }}-{{ row.title }}------[{{ row.article_type.caption }}]-[{{ row.category.caption }}]</li> {% endfor %} </ul> </body> </html> article.html数据库结构:
from django.db import models # Create your models here. class Categoery(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(Categoery) article_type = models.ForeignKey(ArticleType)处理文件:
from . import models def article(request,*args,**kwargs): search_dict = {} for key,value in kwargs.items(): kwargs[key] = int(value) # 把字符类型转化为int类型 方便前端做if a == b 这样的比较 if value !='0': search_dict[key] = value articles = models.Article.objects.filter(**search_dict) # 字典为空时表示搜索所有 article_type = models.ArticleType.objects.all() category = models.Categoery.objects.all() return render(request,'article.html',{'articles':articles, 'article_type':article_type, 'category':category , 'kwargs':kwargs})注:实现此功能并不难,最重要的是理清里面的思路;首先先要确定url访问路径格式http://127.0.0.1:8000/article/0-0.html ,第一个0表示 article_type字段,第二个0表示category字段,如果为零时,表示搜索此字段全部信息,确认好这个,是成功的第一步,处理文件上有检索的处理;第二个关键点是生成字典search_dict进行相关的搜索,如果是0表示搜索全部;第三个关键点,也是很巧妙的一个方式,把参数kwargs再次传到前端,简直神来之笔!
2、另一种尝试(加载内存调优)
由于ArticleType类型是博客定死的数据,后期不会做变动,可以把数据加载到内存当中,加快查询速度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .condition a{ display:inline-block; padding: 3px 5px; border: 1px solid black; } .condition a.active{ background-color: brown; } </style> </head> <body> <h2>过滤条件</h2> <div class="condition"> {% if kwargs.article_type_id == 0 %} <a href="/article/0-{{ kwargs.category_id }}.html" class="active">全部</a> {% else %} <a href="/article/0-{{ kwargs.category_id }}.html">全部</a> {% endif %} {% for row in article_type%} {% if row.0 == kwargs.article_type_id %} <a class="active" href="/article/{{ row.0 }}-{{ kwargs.category_id }}.html">{{ row.1 }}</a> {% else %} <a href="/article/{{ row.0 }}-{{ kwargs.category_id }}.html">{{ row.1 }}</a> {% endif %} {% endfor %} </div> <div class="condition"> {% if kwargs.category_id == 0 %} <a class="active" href="/article/{{ kwargs.article_type_id }}-0.html">全部</a> {% else %} <a href="/article/{{ kwargs.article_type_id }}-0.html">全部</a> {% endif %} {% for row in category %} {% if row.id == kwargs.category_id %} <a class="active" href="/article/{{ kwargs.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a> {% else %} <a href="/article/{{ kwargs.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a> {% endif %} {% endfor %} </div> <h2>查询结果</h2> <ul> {% for row in articles %} <li>{{ row.id }}-{{ row.title }}------[{{ row.article_type }}]-[{{ row.category.caption }}]</li> {% endfor %} </ul> </body> </html> article.html from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def index(request): return HttpResponse('Ok') from . import models def article(request,*args,**kwargs): search_dict = {} for key,value in kwargs.items(): kwargs[key] = int(value) # 把字符类型转化为int类型 方便前端做if a == b 这样的比较 if value !='0': search_dict[key] = value print(kwargs) articles = models.Article.objects.filter(**search_dict) # 字典为空时表示搜索所有 article_type = models.Article.type_choice print(article_type) category = models.Categoery.objects.all() return render(request,'article.html',{'articles':articles, 'article_type':article_type, 'category':category , 'kwargs':kwargs}) 处理文件.py数据库文件:
from django.db import models # Create your models here. class Categoery(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(Categoery) # article_type = models.ForeignKey(ArticleType) type_choice = [ (1,'python'), (2,'linux'), (3,'大数据'), (4,'架构'), ] article_type_id = models.IntegerField(choices=type_choice) 3、利用simple_tag把代码优化关联文件 :
from django.db import models # Create your models here. class Categoery(models.Model): caption = models.CharField(max_length=16) class ArticleTy