【Django】组合筛选

组合筛选

数据库表设计:

from django.db import models

class Direction(models.Model):
    ‘‘‘
    视频方向:全栈、前端、后端、测试、运维、自动化
    ‘‘‘
    name = models.CharField(max_length=32,verbose_name="方向名称")
    m = models.ManyToManyField("Classification")

    # 别名
    class Meta:
        verbose_name_plural = "方向分类"

    def __str__(self):
        return self.name

class Classification(models.Model):
    ‘‘‘
    语言分类:Python、Java、HTML、css、selenium、NodeJs、OpenStack
    ‘‘‘
    name = models.CharField(max_length=32,verbose_name="视频分类")

    #别名
    class Meta:
        verbose_name_plural = "编程语言分类"

    def __str__(self):
        return self.name

class Level(models.Model):
    title = models.CharField(max_length=32)

    class Meta:
        verbose_name_plural = "难度等级"

    def __str__(self):
        return self.title

class Video(models.Model):
    status_choice = (
        (1,"上线"),(2,"下线")
    )
    status = models.IntegerField(choices=status_choice,default=1,verbose_name="状态")
    level = models.ForeignKey("Level",on_delete=models.CASCADE)
    classification = models.ForeignKey("Classification",null=True,on_delete=models.CASCADE,verbose_name="语言分类")
    weight = models.IntegerField(verbose_name="权重(按照从大到小排列)")

    title = models.CharField(max_length=32,verbose_name="标题")
    summary = models.CharField(max_length=512, verbose_name="简介")
    img = models.ImageField(verbose_name="图片",upload_to="static/images")
    href = models.URLField(verbose_name="视频地址",max_length=64)

    class Meta:
        db_table = "Video"
        verbose_name_plural = "视频"

    def __str__(self):
        return self.title

路由分发:

from django.contrib import admin
from django.conf.urls import url
from app01 import views

urlpatterns = [
    url(‘admin/‘, admin.site.urls),
    url(‘^video-(?P<classification_id>\d+)-(?P<level_id>\d+)-(?P<status>\d+).html/$‘, views.video),
    url(‘^video2-(?P<direction_id>\d+)-(?P<classification_id>\d+)-(?P<level_id>\d+).html/$‘, views.video2),
]

版本1、一对多筛选

效果:

视图函数views.py

from django.shortcuts import render
from app01.models import Classification,Direction,Level,Video

def video(request,*args,**kwargs):
    condition = {
        # "classification_id":0
        # "level_id":0,
        # "status":0
    }
    print("kwargs--->",kwargs)      #url的  video-1-1 的分类classification_id=1  level_id=1 保存到kwargs
    # kwargs - --> {‘classification_id‘: ‘1‘, ‘level_id‘: ‘1‘,‘status‘:‘1‘}     #键值对 都是字符串格式
    for k,v in kwargs.items():
        temp = int(v)
        kwargs[k] = temp
        if temp:                    #当temp==0 时,condition=={}  即filter() 过滤条件为空,匹配所有
            condition[k] = temp

    classification_list = Classification.objects.all()
    level_list = Level.objects.all()
    status_list = Video.status_choice
    # status_choice = (
    #     (1, "上线"), (2, "下线")
    # )
    # 在模板语言中 循环体内调用方式为  item.0  item.1      ,这样子很不直观
    # 可以将status_choice 改变为 [{‘id‘:1,‘name‘:‘上线‘},{‘id‘:2,‘name‘:‘下线‘}]
    status_list = list(map(lambda x:{"id":x[0],"name":x[1]},status_list))

    video_list = Video.objects.filter(**condition)

    return render(
        request,
        "video.html",
        {
            "classification_list":classification_list,
            "level_list":level_list,
            "kwargs":kwargs,
            "video_list":video_list,
            "status_list":status_list,
        }
    )

视图函数views.py

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>视频-组合筛选</title>
    <style>
        a.btn{
            font-size: 16px;
            border: 1px solid orange;
            font-weight: 500;
            padding: 10px;
            text-decoration: none;
            border-radius: 5px;
        }
        .row{
            font-size: 20px;
            margin-top: 30px;
        }
        .active{
            background-color: orangered;
            color: white;
            font-weight: 700;
        }
        .resultShow{
            border: 1px dotted darkblue;
        }
        .resultShow div{
            margin: 20px;
        }
    </style>
</head>
<body>
<div class="contents">
    <div class="selectorBox">
        <h1>筛选</h1>
        <div class="classes row">
            <p>编程语言分类:</p>
            {% if kwargs.classification_id == 0 %}
                <a href="/video-0-{{ kwargs.level_id }}-{{ kwargs.status }}.html" class="active btn">全部</a>
            {% else %}
                <a href="/video-0-{{ kwargs.level_id }}-{{ kwargs.status }}.html" class="btn">全部</a>
            {% endif %}
            {% for item in classification_list %}
                {% if item.id == kwargs.classification_id %}
                    <a href="/video-{{ item.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}.html" class="active btn">{{ item.name }}</a>
                {% else %}
                    <a href="/video-{{ item.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}.html" class="btn">{{ item.name }}</a>
                {% endif %}
            {% endfor %}
        </div>
        <div class="level row">
            <p>难度等级:</p>
           {% if kwargs.level_id == 0 %}
                <a href="/video-{{ kwargs.classification_id }}-0-{{ kwargs.status }}.html" class="active btn">全部</a>
            {% else %}
                <a href="/video-{{ kwargs.classification_id }}-0-{{ kwargs.status }}.html" class="btn">全部</a>
            {% endif %}
            {% for item in level_list %}
                {% if item.id == kwargs.level_id %}
                    <a href="/video-{{ kwargs.classification_id }}-{{ item.id }}-{{ kwargs.status }}.html" class="active btn">{{ item.title }}</a>
                {% else %}
                    <a href="/video-{{ kwargs.classification_id }}-{{ item.id }}-{{ kwargs.status }}.html" class="btn">{{ item.title }}</a>
                {% endif %}
            {% endfor %}
        </div>
        <div class="status row">
            <p>上线/下线:</p>
           {% if kwargs.status == 0 %}
                <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-0.html" class="active btn">全部</a>
            {% else %}
                <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-0.html" class="btn">全部</a>
            {% endif %}
            {% for item in status_list %}
                {% if item.id == kwargs.status %}
                    <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-{{ item.id }}.html" class="active btn">{{ item.name }}</a>
                {% else %}
                    <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-{{ item.id }}.html" class="btn">{{ item.name }}</a>
                {% endif %}
            {% endfor %}
        </div>

    </div>
    <div class="resultBox">
        <h1>搜索结果</h1>
        <div class="resultShow">
            {% for video in video_list %}
                <div><span>标题:</span>  <span style="font-size: 26px;font-weight: 700;color: orangered">{{ video.title }}</span></div>
                <div><span>简介:</span>  <span>{{ video.summary }}</span></div>
                <div><span>图片:</span><img src="/static/images/{{ video.img }}" ></div>
                <div><span>视频href:</span>  <a href="{{ video.href }}">{{ video.href }}</a></div>
                <hr>
            {% endfor %}
        </div>
    </div>
</div>

</body>
</html>

  

版本2、多对多筛选

效果:

视图函数views.py

from django.shortcuts import render
from app01.models import Classification,Direction,Level,Video

def video2(request,*args,**kwargs):
    print(kwargs)
    direction_list = Direction.objects.all()

    for k,v in kwargs.items():
        kwargs[k] = int(v)
    direction_id = kwargs["direction_id"]
    classification_id = kwargs["classification_id"]
    condition = {}
    classification_id_list = []

    if direction_id:
        direction_obj = Direction.objects.filter(id=direction_id).first()
        vlist = direction_obj.m.all().values_list("id")

        if vlist:
            classification_id_list = list(zip(*vlist))[0]

        if classification_id == 0:
            condition["classification_id__in"] = classification_id_list
        else:
            if classification_id in classification_id_list:
                condition["classification_id"] = classification_id
            else:
                ###方向id=1时 分类id=[1,2,3] 但是你url传入的分类id=4 不在[1,2,3]中。则classification_id == direction_id所对应的则classification_id
                condition["classification_id__in"] = classification_id_list
                classification_id = 0

        cls_condition = {"id__in": list(classification_id_list)}
        classification_list = Classification.objects.filter(**cls_condition)
    else:
        classification_list = Classification.objects.all()

    if kwargs["level_id"]:
        condition["level_id"] = kwargs["level_id"]

    level_list = Level.objects.all()
    video_list = Video.objects.filter(**condition)

    print(condition,video_list)
    return render(request,"video2.html",{
        "direction_list": direction_list,
        "classification_list": classification_list,
        "level_list": level_list,
        "classification_id":classification_id,
        "kwargs": kwargs,
        "video_list": video_list,
    })

模板HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组合筛选2</title>
    <style>
        a.btn{
            font-size: 16px;
            border: 1px solid orange;
            font-weight: 500;
            padding: 10px;
            text-decoration: none;
            border-radius: 5px;
        }
        .row{
            font-size: 20px;
            margin-top: 30px;
        }
        .active{
            background-color: orangered;
            color: white;
            font-weight: 700;
        }
        .resultShow{
            border: 1px dotted darkblue;
        }
        .resultShow div{
            margin: 20px;
        }
    </style>
</head>
<body>
<div class="contents">
    <div class="selectorBox">
        <h1>筛选</h1>
        <div class="directions row">
            <p>视频方向:</p>
            {% if kwargs.direction_id == 0 %}
                <a href="/video2-0-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html" class="btn active">全部</a>
            {% else %}
                <a href="/video2-0-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html" class="btn">全部</a>
            {% endif %}
            {% for direction in direction_list %}
                {% if kwargs.direction_id == direction.id %}
                    <a href="/video2-{{ direction.id }}-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html" class="btn active">{{ direction.name }}</a>
                {% else %}
                    <a href="/video2-{{ direction.id }}-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html" class="btn">{{ direction.name }}</a>
                {% endif %}
            {% endfor %}
        </div>
        <div class="classifications row">
            <p>语言分类:</p>
            {% if classification_id == 0 %}
                <a href="/video2-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html" class="btn active">全部</a>
            {% else %}
                <a href="/video2-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html" class="btn">全部</a>
            {% endif %}
            {% for item in classification_list %}
                {% if classification_id == item.id %}
                    <a href="/video2-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}.html" class="btn active">{{ item.name }}</a>
                {% else %}
                    <a href="/video2-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}.html" class="btn">{{ item.name }}</a>
                {% endif %}
            {% endfor %}
        </div>
        <div class="levels row">
            <p>难度等级:</p>
            {% if kwargs.level_id == 0 %}
                <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-0.html" class="btn active">全部</a>
            {% else %}
                <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-0.html" class="btn">全部</a>
            {% endif %}
            {% for item in level_list %}
                {% if kwargs.level_id == item.id %}
                    <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ item.id }}.html" class="btn active">{{ item.title }}</a>
                {% else %}
                    <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ item.id }}.html" class="btn">{{ item.title }}</a>
                {% endif %}
            {% endfor %}
        </div>

    </div>
    <div class="resultBox">
        <h1>搜索结果</h1>
        <div class="resultShow">
            {% for video in video_list %}
                <div><span>标题:</span>  <span style="font-size: 26px;font-weight: 700;color: orangered">{{ video.title }}</span></div>
                <div><span>简介:</span>  <span>{{ video.summary }}</span></div>
                <div><span>图片:</span><img src="/static/images/{{ video.img }}" ></div>
                <div><span>视频href:</span>  <a href="{{ video.href }}">{{ video.href }}</a></div>
                <hr>
            {% endfor %}
        </div>
    </div>
</div>

</body>
</html> 

重要知识点总结:

1、路由中正则使用有名分组,传入的信息以字典格式保存在 视图函数的 kwargs中

url(‘^video-(?P<classification_id>\d+)-(?P<level_id>\d+)-(?P<status>\d+).html/$‘, views.video),

-------------------------------------------------------------------------views.py
print("kwargs--->",kwargs)      #url的  video-1-1 的分类classification_id=1  level_id=1 保存到kwargs
# kwargs - --> {‘classification_id‘: ‘1‘, ‘level_id‘: ‘1‘,‘status‘:‘2‘}     #键值对 都是字符串格式

2、map()  函数的用法:

yuanzu = ((1,"在线"),(2,"下线"))

status_list = list(map(lambda x:{"id":x[0],"name":x[1]},yuanzu))
print(status_list)
#[{‘id‘: 1, ‘name‘: ‘在线‘}, {‘id‘: 2, ‘name‘: ‘下线‘}]

s_li = ["beijing",3,"zhejiang",7]
def multi(s):
    if type(s)==int:
        return s**2
    elif type(s) == str:
        return s.upper()
    else:
        return s

print(list(map(lambda x:multi(x),s_li)))
#[‘BEIJING‘, 9, ‘ZHEJIANG‘, 49]

3、zip() 函数用法:

# zip() 方法
v1 = [1,2,3]
v2=[44,55,66]

print(list(zip(v1,v2)))         #二合一
#[(1, 44), (2, 55), (3, 66)]
print(list(zip(*[[11,22,33],[1,5,9]])))     #一分二
#[(11, 1), (22, 5), (33, 9)]
print(list(zip(*[(11,22,33),(44,55,66,77)])))
# [(11, 44), (22, 55), (33, 66)]

4、模板语言 if 判断是否选中,以及for循环遍历

<div class="classes row">
            <p>编程语言分类:</p>
            {% if kwargs.classification_id == 0 %}
                <a href="/video-0-{{ kwargs.level_id }}-{{ kwargs.status }}.html" class="active btn">全部</a>
            {% else %}
                <a href="/video-0-{{ kwargs.level_id }}-{{ kwargs.status }}.html" class="btn">全部</a>
            {% endif %}
            {% for item in classification_list %}
                {% if item.id == kwargs.classification_id %}
                    <a href="/video-{{ item.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}.html" class="active btn">{{ item.name }}</a>
                {% else %}
                    <a href="/video-{{ item.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}.html" class="btn">{{ item.name }}</a>
                {% endif %}
            {% endfor %}
</div>

5、多对多筛选条件判断

def video2(request,*args,**kwargs):
    print(kwargs)
    direction_list = Direction.objects.all()

    #将字典 value值变为整数
    for k,v in kwargs.items():
        kwargs[k] = int(v)
    direction_id = kwargs["direction_id"]
    classification_id = kwargs["classification_id"]
    condition = {}
    classification_id_list = []
    #如果direction_id !=0
    if direction_id:
        direction_obj = Direction.objects.filter(id=direction_id).first()
        vlist = direction_obj.m.all().values_list("id")
        #如果方向id 下对应有分类
        if vlist:
            classification_id_list = list(zip(*vlist))[0]
        #分类id=0
        if classification_id == 0:
            condition["classification_id__in"] = classification_id_list
        else:
            # 分类id 在方向id对应的分类列表中
            if classification_id in classification_id_list:
                condition["classification_id"] = classification_id
            else:
                ###方向id=1时 分类id=[1,2,3] 但是你url传入的分类id=4 不在[1,2,3]中。则classification_id == direction_id所对应的则classification_id
                condition["classification_id__in"] = classification_id_list
                classification_id = 0

        cls_condition = {"id__in": list(classification_id_list)}
        classification_list = Classification.objects.filter(**cls_condition)
    else:
        classification_list = Classification.objects.all()

    if kwargs["level_id"]:
        #如果等级id不为0 则写入条件字典
        condition["level_id"] = kwargs["level_id"]

    level_list = Level.objects.all()
    video_list = Video.objects.filter(**condition)

    print(condition,video_list)
    return render(request,"video2.html",{
        "direction_list": direction_list,
        "classification_list": classification_list,
        "level_list": level_list,
        "classification_id":classification_id,
        "kwargs": kwargs,
        "video_list": video_list,
    })

参考:

https://www.cnblogs.com/ccorz/p/5985205.html

https://www.jianshu.com/p/8402823e8f1b

原文地址:https://www.cnblogs.com/XJT2018/p/11381325.html

时间: 2024-11-11 13:16:22

【Django】组合筛选的相关文章

django 组合搜索

urls: urlpatterns = [ path('admin/', admin.site.urls), path('index/', views.index), re_path(r'^video-(?P<direction_id>(\d+))-(?P<classes_id>(\d+))-(?P<level_id>(\d+)).html$',views.video,name='video2'),#使用关键字正则表达式匹配] models: class Directi

wireshark抓包工具常用筛选命令方法

Wireshark过滤规则使用 一.      MAC地址过滤 命令汇总: eth.addr==20:dc:e6:f3:78:cc eth.src==20:dc:e6:f3:78:cc eth.dst==20:dc:e6:f3:78:cc 1.根据MAC地址进行筛选 使用命令:eth.addr==20:dc:e6:f3:78:cc 命令解说:筛选出MAC地址是20:dc:e6:f3:78:cc的数据包,包括源MAC地址或者目的MAC地址使用的是20:dc:e6:f3:78:cc的全部数据包. 2

#研发解决方案介绍#基于ES的搜索+筛选+排序解决方案

郑昀 基于胡耀华和王超的设计文档 最后更新于2014/12/3 关键词:ElasticSearch.Lucene.solr.搜索.facet.高可用.可伸缩.mongodb.SearchHub.商品中心 本文档适用人员:研发和运维 提纲: 曾经的基于MongoDB的筛选+排序解决方案 MongoDB方案的缺陷 看中了搜索引擎的facet特性 看中了ES的简洁 看中了ES的天生分布式设计 窝窝的ES方案 ES的几次事故和教训 ES自身存在的问题 首先要感谢王超和胡耀华两位研发经理以严谨治学的研究精

python django+uwsgi+nginx 搭建步骤(血一般的教训)

安装Django 没有pip的童鞋可以看http://blog.csdn.net/imphp/article/details/38228619安装python包管理工具pip 先在 https://pypi.python.org/ 上 搜索Django,排在第一的那个就是我们要安装的主要框架Django啦- 点进去可以查找一些相关文档,虽然全部是鸟语花香,不过有翻译.百度.炕忙,就算是菜鸟也成英国鸟了. Django 的官网文档 https://docs.djangoproject.com/en

报障系统之博客主页及后台管理

个人博客: url函数(路由系统): """baozhang URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app i

自定义CRM系统

写在前面 之前在windows上写代码逻辑.搞前端等花了很长时间,跑通之后一直没往centos上部署, 昨天尝试部署下,结果发现静态文件找不到 =='' 由于写了2个组件: - arya model的增删改查,模拟django admin - rbac 基于角色的访问控制 并且每个组件下都有自己的静态文件,层次结构如下: [[email protected] crm_rbac_arya]# tree -I "statics|*pyc|migrations" . -L 3 . ├── a

Asp.net管理信息系统中数据统计功能的实现

数据统计是每个系统中必备的功能,在给领导汇报统计数据,工作中需要的进展数据时非常有用. 在我看来,一个统计的模块应该实现以下功能: 能够将常用的查询的统计结果显示出来: 显示的结果可以是表格形式,也可以是图形形式,如果是图形的话能够以多种形式显示(柱状图.折线图.饼图.雷达图.堆叠柱状图等): 统计查询的结果,点击数字或者百分比能够显示详细的数据: 能够自由组合查询条件.筛选条件.分组条件.排序等: 统计结果最好有个实时预览: 查询统计能够保存,以便下次能直接调用并显示统计查询的结果: 对于保存

浅谈CSS 选择器

A   标签HTML 选择器 body {     padding : 0px ;  margin : 0px ;  background-color : #ffdee0 ; } B   类别CLASS 选择器 <style type="text/css">      .hongkong { color : blue ; } .hunang { color : red ; } </style> ......   <p class="hongkon

问答项目---处理待解决/已回答/高悬赏/零回答!

解决思路: 带解决问题是:0, 已解决问题是:1, 高悬赏是:2, 零回答是:3 通过传递一个参数来判断是那个: <ul class='ans-sel'> <li <if condition='$filter eq 0'>class='on'</if>> <a href="{:U('index',array('id'=>$cateid))}">待解决问题</a> </li> <li <