django 組合搜索
阿新 • • 發佈:2018-07-12
根據 index 全部 char 生成 htm dex zip 搜索
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 Direction(models.Model):
name = models.CharField(max_length=20)
class Meta:
db_table = ‘direction‘
verbose_name_plural = ‘方向‘
def __str__(self):
return self.name
class Classes(models.Model):
name = models.CharField(max_length=20)
classes = models.ManyToManyField(‘Direction‘,related_name=‘classes‘)
class Meta:
db_table = ‘classes‘
verbose_name_plural = ‘課程‘
def __str__(self):
return self.name
class Level(models.Model):
title = models.CharField(max_length=20)
level = models.ManyToManyField(‘Classes‘,related_name=‘level‘)
class Meta:
db_table = ‘level‘
verbose_name_plural = ‘級別‘
def __str__(self):
return self.title
class Video(models.Model):
status = (
(0,‘下線‘),
(1,‘上線‘)
)
video_status = models.IntegerField(choices=status,verbose_name=‘狀態‘)
classes = models.ForeignKey(‘Classes‘,on_delete=models.CASCADE,related_name=‘course‘,verbose_name=‘課程‘)
level = models.ForeignKey(‘Level‘,on_delete=models.CASCADE,related_name=‘grade‘,verbose_name=‘難度級別‘)
wright = models.IntegerField(unique=True,verbose_name=‘權重‘)
title = models.CharField(max_length=20,verbose_name=‘標題‘)
intro = models.CharField(max_length=32,verbose_name=‘簡介‘)
href = models.CharField(max_length=50,verbose_name=‘視頻地址‘)
class Meta:
db_table = ‘video‘
verbose_name_plural = ‘視頻教程‘
def __str__(self):
return self.title
views:
def video(request,*args,**kwargs):#添加關鍵字匹配
container = {} #創建一個空字典用於給視頻字段搜索
for k,v in kwargs.items(): #把kwargs傳過來的關鍵字參數的值轉換為int類型
temp = int(v)
kwargs[k] = temp
direction_id = kwargs.get(‘direction_id‘) #拿到關鍵字參數
classes_id = kwargs.get(‘classes_id‘)
level_id = kwargs.get(‘level_id‘)
direction_list = models.Direction.objects.all() #方向字段
if direction_id == 0: #如果方向為0,列出全部課程
class_list = models.Classes.objects.all()
if classes_id == 0: #如果課程為0,不做任何操作,如果課程不為0把課程ID添加到container字典中
pass
else:
container[‘classes_id‘] = classes_id
if level_id == 0: #如果難度級別為0,不做任何操作,如果不為0把難度級別ID添加到container字典中
pass
else:
container[‘level_id‘] = level_id
else:
direction_obj = models.Direction.objects.filter(id=direction_id).first() #如果方向不為0,根據關鍵字傳過來的ID參數篩選方向對象
class_list = direction_obj.classes.all() #列出方向對象對應的所有課程
v_list = direction_obj.classes.all().values_list(‘id‘) #生成方向對象對應的所有課程的ID列表
if not v_list: #如果沒有匹配到課程ID列表把列表設置為空
classes_id_list = []
else:
classes_id_list = list(zip(*v_list))[0] #如果匹配到了通過ZIP函數生成課程ID列表
if classes_id == 0: #如果課程ID為0 用雙下劃線In方法匹配這個方向下所有的課程ID
container[‘classes_id__in‘] = classes_id_list
else: #如果不為0 課程ID賦值給容器字典
container[‘classes_id‘] = classes_id
if classes_id in classes_id_list: #如果課程ID在匹配到的課程列表內
container[‘classes_id‘] = classes_id
else: #如果課程ID超出範圍
container[‘classes_id__in‘] = classes_id_list
if level_id == 0:
pass
else:
container[‘level_id‘] = level_id
level_list = models.Level.objects.all()
video_list = models.Video.objects.filter(**container) #通過關鍵字解包匹配視頻字段
return render(request,‘video.html‘,locals())
HTML:
<div>
<h1>篩選</h1>
<div>
{% if kwargs.direction_id == 0 %}
<a class="actives" href="/video-0-{{ kwargs.classes_id }}-{{ kwargs.level_id }}.html">全部</a>
{% else %}
<a href="/video-0-{{ kwargs.classes_id }}-{{ kwargs.level_id }}.html">全部</a>
{% endif %}
{% for item in direction_list %}
{% if item.id == kwargs.direction_id %}
<a class="actives" href="/video-{{ item.id }}-{{ kwargs.classes_id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a>
{% else %}
<a href="/video-{{ item.id }}-{{ kwargs.classes_id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a>
{% endif %}
{% endfor %}
</div>
<div>
{% if kwargs.classes_id == 0 %}
<a class="actives" href="/video-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html">全部</a>
{% else %}
<a href="/video-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}.html">全部</a>
{% endif %}
{% for item in class_list %}
{% if item.id == kwargs.classes_id %}
<a class="actives" href="/video-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a>
{% else %}
<a href="/video-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a>
{% endif %}
{% endfor %}
</div>
<div>
{% if kwargs.level_id == 0 %}
<a class="actives" href="/video-{{ kwargs.direction_id }}-{{ kwargs.classes_id }}-0.html">全部</a>
{% else %}
<a href="/video-{{ kwargs.direction_id }}-{{ kwargs.classes_id }}-0.html">全部</a>
{% endif %}
{% for item in level_list %}
{% if item.id == kwargs.level_id %}
<a class="actives" href="/video-{{ kwargs.direction_id }}-{{ kwargs.classes_id }}-{{ item.id }}.html">{{ item.title}}</a>
{% else %}
<a href="/video-{{ kwargs.direction_id }}-{{ kwargs.classes_id }}-{{ item.id }}.html">{{ item.title}}</a>
{% endif %}
{% endfor %}
</div>
</div>
<div>
<h1>結果</h1>
<div>
{% for item in video_list %}
<p>{{ item.title }}</p>
{% endfor %}
</div>
</div>
django 組合搜索