django 组合搜索

Posted louzi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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 组合搜索的主要内容,如果未能解决你的问题,请参考以下文章

Django实现组合搜索的方法示例

Django实现组合搜索的方法示例

Django 之组合搜索

django 组合搜索

Django实现组合搜索

django-jsonp 瀑布流 组合搜索 多级评论