1
2:组合搜索
2.1 创建model类
from django.db import models class Direction(models.Model): """ 方向:自动化、测试、运维、前端 verbose_name Admin中显示的字段名称 """ name=models.CharField(verbose_name='名称',max_length=32) classification = models.ManyToManyField("Classification") # Django模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性。 class Meta: # db_table是指定自定义数据库表明的。 定义该model在数据库中的表名称 db_table="Direction" # 这个选项是指定,模型的复数形式是什么,如果不指定Django会自动在模型名称后加一个’s’ verbose_name_plural='方向(视频方向)' def __str__(self): return self.name class Classification(models.Model): ''' 分类:Python Linux Java js ''' name = models.CharField(verbose_name='名称',max_length=32) class Meta: db_table='Classification' 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=( (0,'下线'), (1,'上线'), ) status=models.IntegerField(verbose_name='状态',choices=status_choice,default=1) level=models.ForeignKey(Level,on_delete=models.CASCADE) classification = models.ForeignKey('Classification',on_delete=models.CASCADE,null=True,blank=True) weight=models.IntegerField(verbose_name='权重(按从大到小排列)',default=0) title=models.CharField(verbose_name='标题',max_length=32) summary = models.CharField(verbose_name='简介',max_length=32) img=models.CharField(verbose_name='图片',max_length=32) href=models.CharField(verbose_name='视频地址',max_length=256) create_date=models.DateTimeField(auto_created=True) class Meta: db_table='Video' verbose_name_plural='视频' def __str__(self): return self.title
2.2 创建数据表+创建超级管理员
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
2.3 修改admin.py 文件,将要管理的表注册到系统中
from django.contrib import admin from app01 import models # Register your models here. admin.site.register(models.Direction) admin.site.register(models.Classification) admin.site.register(models.Level) admin.site.register(models.Video)
2.4 进行数据的录入和维护
方向表:自动化、测试、运维、前端
分类:python、Java、js、Linux
级别:初级、高级、骨灰级
视频:python初体验
2.5 组合搜素(主外键)
"""Enterprise URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^video-(?P<classification_id>(d+))-(?P<level_id>(d+))-(?P<status>(d+)).html$',views.video) ]
from django.shortcuts import render from app01 import models # Create your views here. def video(request,*args,**kwargs): condition={ #'classification_id':0 #'level_id':1 #'status':1 } for k,v in kwargs.items(): temp = int(v) kwargs[k] = temp if temp: condition[k]=temp print(condition) class_list = models.Classification.objects.all() level_list = models.Level.objects.all() status_list = list(map(lambda x:{'id':x[0],'name':x[1]},models.Video.status_choice)) video_list = models.Video.objects.filter(**condition) return render(request,"VideoTest/video.html",{ 'class_list':class_list, 'level_list':level_list, 'status_list':status_list, 'kwargs':kwargs, 'video_list':video_list, })
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .condition a.active { background-color: coral; color: white; } </style> </head> <body> <div class="condition"> <h1>筛选</h1> <div> {% if kwargs.classification_id == 0 %} <a class="active" href="/video-0-{{ kwargs.level_id }}-{{ kwargs.status }}.html">全部</a> {% else %} <a href="/video-0-{{ kwargs.level_id }}-{{ kwargs.status }}.html">全部</a> {% endif %} {% for item in class_list %} {% if kwargs.classification_id == item.id %} <a class="active" href="/video-{{ item.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}.html">{{ item.name }}</a> {% else %} <a href="/video-{{ item.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}.html">{{ item.name }}</a> {% endif %} {% endfor %} </div> <div> <a href="/video-{{ kwargs.classification_id }}-0-{{ kwargs.status }}.html">全部</a> {% for item in level_list %} {% if item.id == kwargs.level_id %} <a href="/video-{{ kwargs.classification_id }}-{{ item.id }}-{{ kwargs.status }}.html" class="active">{{ item.title }}</a> {% else %} <a href="/video-{{ kwargs.classification_id }}-{{ item.id }}-{{ kwargs.status }}.html">{{ item.title }}</a> {% endif %} {% endfor %} </div> <div> <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-0.html">全部</a> {% for item in status_list %} {% if item.id == kwargs.status %} <a class="active" href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-{{ item.id }}.html">{{ item.name }}</a> {% else %} <a href="/video-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-{{ item.id }}.html">{{ item.name }}</a> {% endif %} {% endfor %} </div> </div> <div> <h1>结果</h1> <div> {% for row in video_list %} <div>{{ row.title }}</div> {% endfor %} </div> </div> </body> </html>
2.6 组合搜素(多对多)
"""Enterprise URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^video-(?P<classification_id>(d+))-(?P<level_id>(d+))-(?P<status>(d+)).html$',views.video), url(r'^video2-(?P<direction_id>(d+))-(?P<classification_id>(d+))-(?P<level_id>(d+)).html$', views.video2) ]
from django.shortcuts import render from app01 import models # Create your views here. def video(request, *args, **kwargs): condition = { # 'classification_id':0 # 'level_id':1 # 'status':1 } for k, v in kwargs.items(): temp = int(v) kwargs[k] = temp if temp: condition[k] = temp print(condition) class_list = models.Classification.objects.all() level_list = models.Level.objects.all() status_list = list(map(lambda x: {'id': x[0], 'name': x[1]}, models.Video.status_choice)) video_list = models.Video.objects.filter(**condition) return render(request, "VideoTest/video.html", { 'class_list': class_list, 'level_list': level_list, 'status_list': status_list, 'kwargs': kwargs, 'video_list': video_list, }) def video2(request, *args, **kwargs): condition = { # 'direction':1 # 'classification_id':0 # 'level_id':1 } for k, v in kwargs.items(): temp = int(v) kwargs[k] = temp direction_id = kwargs.get('direction_id') classification_id = kwargs.get('classification_id') level_id = kwargs.get('level_id') direction_list = models.Direction.objects.all() if direction_id ==0 : # 表示方向选择的是“全部”-百无禁忌 class_list = models.Classification.objects.all() if classification_id ==0 : pass else: # 加入到条件字典中 condition["classification_id"]=classification_id else : direction_obj = models.Direction.objects.filter(id=direction_id).first() # 选择该方向下的所有分类 class_list = direction_obj.classification.all() #选择分类的所有id class_list_v_id = direction_obj.classification.all().values_list('id') if not class_list_v_id: clssification_id_list = [] else: clssification_id_list = list(zip(*class_list_v_id))[0] if classification_id == 0: # 说明选择了该方向=====》所有分类 condition['classification_id__in'] = clssification_id_list else: # 说明选择了一个分类,但是要判断该分类是否包含着该方向下 if classification_id in clssification_id_list: condition['classification_id'] = classification_id else: # 说明该分类下并没有在当前选中的方向下 kwargs["classification_id"]=0 condition['classification_id__in'] = clssification_id_list if level_id == 0: pass else: condition['level_id'] = level_id print(condition) level_list = models.Level.objects.all() video_list = models.Video.objects.filter(**condition) return render(request, "VideoTest/video2.html", { 'direction_list': direction_list, 'class_list': class_list, 'level_list': level_list, 'kwargs': kwargs, 'video_list': video_list, })
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .condition a.active { background-color: coral; color: white; } </style> </head> <body> <div class="condition"> <h1>筛选</h1> <div> <a href="/video2-0-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html">全部</a> {% for item in direction_list %} {% if item.id == kwargs.direction_id %} <a class="active" href="/video2-{{ item.id }}-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a> {% else %} <a href="/video2-{{ item.id }}-{{ kwargs.classification_id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a> {% endif %} {% endfor %} </div> <div> {% if kwargs.classification_id == 0 %} <a class="active" href="/video2-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html">全部</a> {% else %} <a href="/video2-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html">全部</a> {% endif %} {% for item in class_list %} {% if kwargs.classification_id == item.id %} <a class="active" href="/video2-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a> {% else %} <a href="/video2-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a> {% endif %} {% endfor %} </div> <div> <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-0.html">全部</a> {% for item in level_list %} {% if item.id == kwargs.level_id %} <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ item.id }}.html" class="active">{{ item.title }}</a> {% else %} <a href="/video2-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ item.id }}.html">{{ item.title }}</a> {% endif %} {% endfor %} </div> </div> <div> <h1>结果</h1> <div> {% for row in video_list %} <div>{{ row.title }}</div> {% endfor %} </div> </div> </body> </html>