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

model类

   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)

admin.py

  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)
]

url

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,
})

Views

<!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>

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)
]

url

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,
})

Views

<!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>

html

饮冰三年-人工智能-Python-28 企业官网(组合搜索)的更多相关文章

  1. 饮冰三年-人工智能-Python-22 Python初识Django

    1:一个简单的web框架 # 导包 from wsgiref.simple_server import make_server #自定义个处理函数 def application(environ,st ...

  2. 饮冰三年-人工智能-Python-21 Python数据库MySql

    一:下载与安装 1:下载地址:https://dev.mysql.com/downloads/mysql/ 2:安装MySql 打开下载文件解压到指定文件目录.(我这里解压目录为D:\MySql\my ...

  3. 饮冰三年-人工智能-Python-20 Python线程、进程、线程

    进程:最小的数据单元 线程:最小的执行单元 一: 1:线程1 import threading #线程 import time def Music(): print("Listen Musi ...

  4. 饮冰三年-人工智能-Python-19 Python网络编程

    Socket:套接字.作用:我们只需要安照socket的规定去编程,就不需要深入理解tcp/udp协议也可以实现 1:TCP协议 1.1  客户端服务端循环收发消息 # 1:引入stock模块(导包) ...

  5. 饮冰三年-人工智能-Python-10之C#与Python的对比

    1:注释 C# 中 单行注释:// 多行注释:/**/ python 中 单行注释:# 多行注释:“““内容””” 2:字符串 C#中 "" 用双引号如("我是字符串&q ...

  6. 饮冰三年-人工智能-linux-08 软件包管理(Python的安装)

    1:软件包存放的位置 media/CentOS_6.9_Final/Packages文件夹下 2.RPM就是Red Hat Package Manger(红帽软件包管理工具)的缩写. 2.1 常用的命 ...

  7. 饮冰三年-人工智能-Python-30 python开发中常见的错误

    1:触发条件:创建的实体类生成到数据库表时报错 报错信息:TypeError: __init__() missing 1 required positional argument: 'on_delet ...

  8. 饮冰三年-人工智能-Python-23 Python PyCharm 使用中常见的问题

    一:软件工具使用中遇到的问题 1:AttributeError: module 'pip' has no attribute 'main'问题 处理方法: a:找到JetBrains\PyCharm ...

  9. 饮冰三年-人工智能-Python-18Python面向对象

    1 类与实例对方法和属性的修改 class Chinese: # 这是一个Chinese的类 #定义一个类属性,又称为静态数据或者静态变量,相当于C#中的static country="Ch ...

随机推荐

  1. Dlib Opencv cv2.fitEllipse用于人眼轮廓椭圆拟合

    dlib库的安装以及人脸特征点的识别分布分别在前两篇博文里面 Dlib Python 检测人脸特征点 Face Landmark Detection Mac OSX下安装dlib (Python) 这 ...

  2. <Android基础> (六) 数据存储 Part 3 SQLite数据库存储

    6.4 SQLite数据库存储 SQLite是一种轻量级的关系型数据库,运算速度快,占用资源少. 6.4.1 创建数据库 Android为了管理数据库,专门提供了SQLiteOpenHelper帮助类 ...

  3. Eureka 配置

    #是否向服务注册中心注册自己,该值默认为trueeureka.client.register-with-eureka=falseserver端建议设为false #服务注册中心的配置内容,指定服务注册 ...

  4. css进度条

    1.环形进度条 源码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  5. SpringBoot实现优雅的关机

    最近在公司使用了 Springboot 项目, 发现在   linux  上 通过 java -jar 命令可以十分安全的运行, 但是 当我们需要关闭它的时候呢? 难道  登陆服务器 kill 线程? ...

  6. TF用法指导

    一.变量 1 .tf.Variables() 功能:创建变量 2.tf.get_variable() 功能:创建变量,注意和上面的区别 3.tf.variable_scope() 功能:其实只要记住一 ...

  7. 20155324《网络对抗》Exp1 PC平台逆向破解(5)M

    20155324<网络对抗>Exp1 PC平台逆向破解(5)M 实验目标 本次实践的对象是一个名为~pwn1~的~linux~可执行文件. 该程序正常执行流程是:~main~调用~foo~ ...

  8. C-Free 5.0 注册码

    用户名:123123 电子邮件:111@qq.com 注册码:mJ2Em9jdm7jGwYTpmp2H6KmehtvO 经过验证,可以正常注册.

  9. C#使用Selenium+PhantomJS抓取数据

    本文主要介绍了C#使用Selenium+PhantomJS抓取数据的方法步骤,具有很好的参考价值,下面跟着小编一起来看下吧 手头项目需要抓取一个用js渲染出来的网站中的数据.使用常用的httpclie ...

  10. 查询sql 索引

    SELECT indexname = a.name , tablename = c. name , indexcolumns = d .name , a .indidFROM sysindexes a ...