【python】组合搜索
实现原理
通过url地址拼接,0-0-0,三个0分别代表一个搜索条件,再用reverse反向查找三个参数,从kwargs里调用值,在自定义函数中自定义url,并生成动态添加的a标签。
效果图
url
from django.conf.urls import url from django.contrib import admin from app010 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^video-(?P<d_to_c_id>\d+)-(?P<cg_id>\d+)-(?P<lv_id>\d+).html',views.new_video,name="www"), ]
models
from django.db import models # Create your models here. class Level(models.Model): name = models.CharField(max_length=32) class Category(models.Model): name = models.CharField(max_length=32) class Direction(models.Model): name = models.CharField(max_length=32) dire_to_cate = models.ManyToManyField("Category") class Video(models.Model): lv = models.ForeignKey(Level) cg = models.ForeignKey(Category) title = models.CharField(verbose_name='标题', max_length=32) summary = models.CharField(verbose_name='简介', max_length=32) img = models.ImageField(verbose_name='图片', upload_to='./static/images/Video/') href = models.CharField(verbose_name='视频地址', max_length=256) create_date = models.DateTimeField(auto_now_add=True)
views
from django.shortcuts import render # Create your views here. from app010 import models def new_video(request,*args,**kwargs): d_to_c_id = kwargs.get("d_to_c_id") cg_id = kwargs.get("cg_id") lv_id = kwargs.get("lv_id") conditions = {} dire_to_cate_list = models.Direction.objects.all() level_list = models.Level.objects.all() if d_to_c_id == "0": #未选择方向 category_list = models.Category.objects.all() if cg_id == "0": #未选择分类 pass else: #选择分类 conditions["cg_id"] = cg_id else: #选择了方向 category_list = models.Category.objects.filter(direction=d_to_c_id) temp = category_list.values_list("id") cg_id_list = list(zip(*temp))[0] if cg_id == "0": #未选择分类 conditions["cg_id__in"] = cg_id_list else: #选择了分类 if int(cg_id) in cg_id_list: conditions["cg_id"] = cg_id else: conditions["cg_id__in"] = cg_id_list kwargs["cg_id"] = "0" if lv_id == "0": pass else: conditions["lv_id"] = lv_id result = models.Video.objects.filter(**conditions) return render(request,"new_video.html",{"dire_to_cate_list":dire_to_cate_list, "category_list": category_list, "level_list":level_list, "arg_list": kwargs, "result": result, })
tags(自定义函数)
#!/usr/bin/env python # coding:utf-8 #author:zhangw from django.utils.safestring import mark_safe from django import template register = template.Library() @register.simple_tag def category_tag(obj,arg_dict): from django.urls import reverse url = reverse("www", kwargs={"cg_id": obj.id, "lv_id": arg_dict.get("lv_id"),"d_to_c_id":arg_dict.get("d_to_c_id")}) if str(obj.id) == arg_dict.get("cg_id"): tag = "<a class='active' href='%s'>%s</a>" %(url,obj.name) return mark_safe(tag) else: tag = "<a href='%s'>%s</a>" %(url,obj.name) return mark_safe(tag) @register.simple_tag def level_tag(obj,arg_dict): from django.urls import reverse url = reverse("www", kwargs={"cg_id": arg_dict.get("cg_id"), "lv_id": obj.id,"d_to_c_id":arg_dict.get("d_to_c_id")}) if str(obj.id) == arg_dict.get("lv_id"): tag = "<a class='active' href='%s'>%s</a>" %(url,obj.name) return mark_safe(tag) else: tag = "<a href='%s'>%s</a>" %(url,obj.name) return mark_safe(tag) @register.simple_tag def total_tag(arg_dict,key): from django.urls import reverse if key == "cg_id": url = reverse("www",kwargs={"cg_id":0,"lv_id":arg_dict.get("lv_id"),"d_to_c_id":arg_dict.get("d_to_c_id")}) elif key == "lv_id": url = reverse("www",kwargs={"cg_id":arg_dict.get("cg_id"),"lv_id":0,"d_to_c_id":arg_dict.get("d_to_c_id")}) elif key == "d_to_c_id": url = reverse("www",kwargs={"cg_id":arg_dict.get("cg_id"),"lv_id":arg_dict.get("lv_id"),"d_to_c_id":0}) else: url = "" if arg_dict.get(key) == "0": tag = "<a class='active' href='%s'>全部</a>" % (url,) else: tag = "<a href='%s'>全部</a>" %(url,) return mark_safe(tag) @register.simple_tag def direction_tag(obj,arg_dict): from django.urls import reverse url = reverse("www",kwargs={"d_to_c_id":obj.id,"cg_id":arg_dict.get("cg_id"),"lv_id":arg_dict.get("lv_id")}) if str(obj.id) == arg_dict.get("d_to_c_id"): tag = "<a class='active' href='%s'>%s</a>" %(url,obj.name) return mark_safe(tag) else: tag = "<a href='%s'>%s</a>" %(url,obj.name) return mark_safe(tag)
前端HTML
{% load tags %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin-left: 20px; } .filter a { margin-left: 20px; text-decoration: none; } .filter a.active { color: red; } </style> </head> <body> <div class="filter"> <h1 style="margin-left: 20px">搜索条件</h1> <div> {% total_tag arg_list "d_to_c_id" %} {% for i in dire_to_cate_list %} {% direction_tag i arg_list %} {% endfor %} </div> <div> {% total_tag arg_list "cg_id" %} {% for i in category_list %} {% category_tag i arg_list %} {% endfor %} </div> <div> {% total_tag arg_list "lv_id" %} {% for i in level_list %} {% level_tag i arg_list %} {% endfor %} </div> </div> <h1 style="margin-left: 20px">查询结果</h1> <div class="content"> {% for row in result %} <div style="width: 100px;height: 220px;float: left"> <img src="{{ row.img }}" style="width: 100px;height: 150px;"> <p style="background-color: aqua">{{ row.title }}</p> <p style="background-color: cadetblue">{{ row.summary }}</p> </div> {% endfor %} </div> </body> </html>
人生短短数十载,经不起几次重头再来