組合搜索
阿新 • • 發佈:2019-04-25
tle temp ice 級別 urn ack active set html
一:
>>>models class Direction(models.Model): name = models.CharField(max_length=32,verbose_name=‘名稱‘) classification = models.ManyToManyField(‘Classification‘) class Meta: verbose_name_plural=‘方向‘ def __str__(self): return self.name class Classification(models.Model): name= models.CharField(max_length=32,verbose_name=‘名稱‘) class Meta: 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 = ( (1,‘下線‘), (2,‘上線‘), ) status = models.IntegerField(verbose_name=‘狀態‘,choices=status_choice,default=1) level = models.ForeignKey(Level,on_delete=True) classification = models.ForeignKey(‘Classification‘,on_delete=True,null=True,blank=True) title = models.CharField(max_length=32,null=True) class Meta: verbose_name_plural = ‘視頻‘ def __str__(self): return self.title >>>urls re_path(‘aselect/-(?P<classification_id>(\d+))-(?P<level_id>(\d+))-(?P<status>(\d+))$‘, views.aselect), >>>html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .condiction a{ display: inline-block; padding: 5px 8px; border: 1px solid black; text-decoration: none; } .condiction a.active{ background-color: #eb9316; color: white; } </style> </head> <body> <div class="condiction"> <h1>篩選</h1> <div> {% if kwargs.classification_id == 0 %} <a class="active" href="/aselect/-0-{{ kwargs.level_id }}-{{ kwargs.status }}">全部</a> {% else %} <a href="/aselect/-0-{{ kwargs.level_id }}-{{ kwargs.status }}">全部</a> {% endif %} {% for item in class_list %} {% if item.id == kwargs.classification_id %} <a class="active" href="/aselect/-{{ item.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}">{{ item.name }}</a> {% else %} <a href="/aselect/-{{ item.id }}-{{ kwargs.level_id }}-{{ kwargs.status }}">{{ item.name }}</a> {% endif %} {% endfor %} </div> <div> {% if kwargs.level_id == 0 %} <a class="active" href="/aselect/-{{ kwargs.classification_id }}-0-{{ kwargs.status }}">全部</a> {% else %} <a href="/aselect/-{{ kwargs.classification_id }}-0-{{ kwargs.status }}">全部</a> {% endif %} {% for item in level_list %} {% if item.id == kwargs.level_id %} <a class="active" href="/aselect/-{{ kwargs.classification_id }}-{{ item.id }}-{{ kwargs.status }}">{{ item.title }}</a> {% else %} <a href="/aselect/-{{ kwargs.classification_id }}-{{ item.id }}-{{ kwargs.status }}">{{ item.title }}</a> {% endif %} {% endfor %} </div> <div> {% if kwargs.status == 0 %} <a class="active" href="/aselect/-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-0">全部</a> {% else %} <a href="/aselect/-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-0"}>全部</a> {% endif %} {% for item in status_list%} {% if kwargs.status == item.id %} <a class="active" href="/aselect/-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-{{ item.id }}">{{ item.name }}</a> {% else %} <a href="/aselect/-{{ kwargs.classification_id }}-{{ kwargs.level_id }}-{{ item.id }}">{{ item.name }}</a> {% endif %} {% endfor%} </div> <h2>結果</h2> <div> {% for row in video_list %} <div>{{ row.title }}</div> {% endfor %} </div> </div> </body> </html> >>>views def aselect(request,*args,**kwargs): condition = {} for k,v in kwargs.items(): temp = int(v) kwargs[k] = temp if temp: condition[k] = temp video_list = Video.objects.filter(**condition) class_list = Classification.objects.all() level_list = Level.objects.all() status_list = list(map(lambda x:{‘id‘:x[0],‘name‘:x[1]},Video.status_choice)) return render(request,‘aselect.html‘,locals())
二:
>>>models class Direction(models.Model): name = models.CharField(max_length=32,verbose_name=‘名稱‘) classification = models.ManyToManyField(‘Classification‘) class Meta: verbose_name_plural=‘方向‘ def __str__(self): return self.name class Classification(models.Model): name = models.CharField(max_length=32,verbose_name=‘名稱‘) class Meta: 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 = ( (1,‘下線‘), (2,‘上線‘), ) status = models.IntegerField(verbose_name=‘狀態‘,choices=status_choice,default=1) level = models.ForeignKey(Level,on_delete=True) classification = models.ForeignKey(‘Classification‘,on_delete=True,null=True,blank=True) title = models.CharField(max_length=32,null=True) class Meta: verbose_name_plural = ‘視頻‘ def __str__(self): return self.title >>>urls from django.urls import re_path re_path(‘aselect2/-(?P<direction_id>(\d+))-(?P<classification_id>(\d+))-(?P<level_id>(\d+))$‘, views.aselect2,name=‘aselect2‘), >>>html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .condiction a{ display: inline-block; padding: 5px 8px; border: 1px solid black; text-decoration: none; } .condiction a.active{ background-color: #eb9316; color: white; } </style> </head> <body> <div class="condiction"> <h1>篩選</h1> <div> {% if kwargs.direction_id == 0 %} <a class="active" href="{% url "aselect2" direction_id=0 classification_id=kwargs.classification_id level_id=kwargs.level_id %}">全部</a> {% else %} <a href="{% url "aselect2" direction_id=0 classification_id=kwargs.classification_id level_id=kwargs.level_id %}">全部</a> {% endif %} {% for item in direction_list %} {% if item.id == kwargs.direction_id %} <a class="active" href="{% url "aselect2" direction_id=item.id classification_id=kwargs.classification_id level_id=kwargs.level_id %}">{{ item.name}}</a> {% else %} <a href="{% url "aselect2" direction_id=item.id classification_id=kwargs.classification_id level_id=kwargs.level_id %}">{{ item.name}}</a> {% endif %} {% endfor %} </div> <div> {% if kwargs.classification_id == 0 %} <a class="active" href="/aselect2/-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}">全部</a> {% else %} <a href="/aselect2/-{{ kwargs.direction_id }}-0-{{ kwargs.level_id }}">全部</a> {% endif %} {% for item in class_list %} {% if item.id == kwargs.classification_id %} <a class="active" href="/aselect2/-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}">{{ item.name}}</a> {% else %} <a href="/aselect2/-{{ kwargs.direction_id }}-{{ item.id }}-{{ kwargs.level_id }}">{{ item.name}}</a> {% endif %} {% endfor %} </div> <div> {% if kwargs.level_id == 0 %} <a class="active" href="/aselect2/-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-0">全部</a> {% else %} <a href="/aselect2/-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-0">全部</a> {% endif %} {% for item in level_list %} {% if item.id == kwargs.level_id %} <a class="active" href="/aselect2/-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ item.id }}">{{ item.title}}</a> {% else %} <a href="/aselect2/-{{ kwargs.direction_id }}-{{ kwargs.classification_id }}-{{ item.id }}">{{ item.title}}</a> {% endif %} {% endfor %} </div> </div> <div> <h1>結果</h1> {% for row in video_list %} <div>{{ row.title }}</div> {% endfor %} </div> </body> </html> def aselect2(request,*args,**kwargs): condition = {} 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 = Direction.objects.all() level_list = Level.objects.all() if direction_id == 0: class_list = Classification.objects.all() if classification_id == 0: pass else: condition[‘classification_id‘] = classification_id else: direction_obj = Direction.objects.filter(id=direction_id).first() class_list = direction_obj.classification.all() v_list = direction_obj.classification.all().values_list(‘id‘) if not v_list: classification_id__in = [] else: calssification_id_list = list(zip(*v_list))[0] if classification_id == 0: condition[‘classification_id__in‘] = calssification_id_list else: if classification_id in calssification_id_list: condition[‘classification_id‘] = classification_id else: kwargs[‘classification_id‘] = 0 condition[‘classification_id__in‘] = calssification_id_list if level_id == 0: pass else: condition[‘level_id‘] = level_id level_list = Level.objects.all() video_list = Video.objects.filter(**condition) return render(request,‘aselect2.html‘,locals())
組合搜索