对数据的筛选功能
路径导航直接使用组定义
url(r'^video-(?P<f_classification_id>(\d+))-(?P<f_level_id>(\d+))-(?P<status>(\d+)).html$', views.video), url(r'^videoSecond-(?P<direction_id>(\d+))-(?P<f_classification_id>(\d+))-(?P<f_level_id>(\d+))-(?P<status>(\d+)).html$', views.videoSecond,name='videoSecond'),
方向和课程是多对多关系
课程和等级和视频是一对多关系
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Video(models.Model): status_choice=( (1,'上线'), (2,'下线'), ) status=models.IntegerField(choices=status_choice,verbose_name='状态',default=1) f_level=models.ForeignKey('Level',on_delete=models.CASCADE,default='') f_classification=models.ForeignKey('Classification',on_delete=models.CASCADE,default='') 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=32) create_time=models.DateTimeField(verbose_name='创建时间',auto_now_add=True) class Meta: db_table = 'Video' verbose_name_plural = '视频' def __str__(self): return self.title
""" 如果:direction_id 0 *列出所有的分类 如果 f_classification_id = 0: pass else: condition['f_classification_id'] = f_classification_id 否则:direction_id != 0 *列表当前方向下的所有分类 如果 f_classification_id = 0: 获取当前方向下的所有分类 [1,2,3,4] condition['f_classification_id__in'] = [1,2,3,4] else: f_classification_id != 0 获取当前方向下的所有分类 [1,2,3,4] f_classification_id 是否在 [1,2,3,4] : condition['f_classification_id'] = f_classification_id else: condition['f_classification_id__in'] = [1,2,3,4] """
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def videoSecond(request,*args,**kwargs): conditions={} #为了后面视频的查找筛选 for k,v in kwargs.items(): temp=int(v) kwargs[k]=temp #kwargs代表导航路径传递的值 direction_id=kwargs.get('direction_id') f_classification_id=kwargs.get('f_classification_id') f_level_id=kwargs.get('f_level_id') direction_list = models.Direction.objects.all() #方向是否为0 if direction_id==0: class_list=models.Classification.objects.all() if f_classification_id==0: pass else: conditions['f_classification_id']=f_classification_id #有课程就显示该课程,无不处理 else: direction_obj=models.Direction.objects.filter(id=direction_id).first() #找到选择方向的对象 class_list=direction_obj.dc.all() #获取该方向下的所有课程列表 classId_list=direction_obj.dc.all().values_list('id') #获取该方向下的所有课程列表id if not classId_list: classification_id_list=[] else: classification_id_list=list(zip(*classId_list))[0] #获取课程所有的id if f_classification_id == 0: conditions['f_classification_id__in']=classification_id_list else: if f_classification_id in classification_id_list: #查找点击的课程是否在课程列表中 conditions['f_classification_id']=f_classification_id else: kwargs['f_classification_id']=0 #切换个方向,课程显示所有的 conditions['f_classification_id__in'] = classification_id_list if f_level_id ==0: pass else: conditions['f_level_id']=f_level_id level_list=models.Level.objects.all() video_list=models.Video.objects.filter(**conditions) status_list=list(map(lambda x:{'id':x[0],'name':x[1]},models.Video.status_choice)) print(kwargs['f_classification_id']) return render( request, 'videoSecond.html', { 'class_list':class_list, 'level_list':level_list, 'video_list':video_list, 'status_list':status_list, 'direction_list':direction_list, 'kwargs':kwargs } )
前端显示路径用多个数字代表不同含义
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<div> {% if kwargs.direction_id == 0 %} <a href="/videoSecond-0-{{ kwargs.f_classification_id }}-{{ kwargs.f_level_id }}-{{ kwargs.status }}.html" class="active">全部</a> {% else %} <a href="/videoSecond-0-{{ kwargs.f_classification_id }}-{{ kwargs.f_level_id }}-{{ kwargs.status }}.html">全部</a> {% endif %} {% for item in direction_list %} {% if item.id == kwargs.direction_id %} <a href="/videoSecond-{{ item.id }}-{{ kwargs.f_classification_id }}-{{ kwargs.f_level_id }}-{{ kwargs.status }}.html" class="active">{{ item.name }}</a> {% else %} <a href="/videoSecond-{{ item.id }}-{{ kwargs.f_classification_id }}-{{ kwargs.f_level_id }}-{{ kwargs.status }}.html">{{ item.name }}</a> {% endif %} {% endfor %} </div>