码迷,mamicode.com
首页 > 其他好文 > 详细

三个方向组合的筛选

时间:2019-09-15 12:58:50      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:float   url   sum   end   code   rgs   通过   col   summary   

#url=video2.0-0-0.html,是按按照,0,1这些来排序的

url(r^video2-(?P<dr_id>\d+)-(?P<level_id>\d+)-(?P<category_id>\d+).html,views.video2,name=table_video2),
#video/views.py
def video2(request,*args,**kwargs):
    #0-0-0.html
    #1-0-0.html
    #1-1-0.html
    dr_id = kwargs.get("dr_id")
    cg_id = kwargs.get("category_id")
    lv_id = kwargs.get("level_id")
    condition = {}
    direction_list = models.Direction.objects.all()
    level_list = models.Level.objects.all()
    if dr_id == "0":#未选择方向

        category_list = models.Category.objects.all()
        if cg_id == "0":#未选择分类
            pass
        else: #未选择方向,选择了分类
            condition["category_id"] =cg_id
    else:#选择方向
        category_list = models.Category.objects.filter(direction=dr_id)
        temp=category_list.values_list("id")
        #print(v)
        cg_id_list = list(zip(*temp))[0]
        #print(cg_id_list)
        if cg_id == "0":#选择了方向没有选择分类
            condition["category_id__in"]=cg_id_list
        else: #选择了方向也选择了分类
            if int(cg_id) in cg_id_list:
                condition["category_id"]=cg_id
            else:#请求的分类不在所有的分类中
                condition["category_id__in"] = cg_id_list
                kwargs["category_id"] = "0"
    if lv_id == "0":
        pass
    else:
        condition["level_id"]=lv_id
    result = models.Video.objects.filter(**condition)
    print(result)


    return render(request,"video/video2.html",{"direction_list":direction_list,
                                               "level_list":level_list,
                                               "category_list":category_list,
                                               "arg_list":kwargs})

利用reverse通过url的name获取url

#video_tags.py
#三个条件

from django.urls import reverse
from django.utils.safestring import mark_safe
@register.simple_tag
def direction_tag(direction,arg_dic):
    """
    方向
    :param catagory:
    :param arg_dic:
    :return:
    """
    url = reverse("table_video2",kwargs={"dr_id":direction.id,"level_id":arg_dic.get("level_id"),"category_id":arg_dic.get("category_id")})
    if str(direction.id) == arg_dic.get("dr_id"):

        tag = ‘‘‘<a href="%s" style="color:red">%s</a>‘‘‘%(url,direction.name)
        return mark_safe(tag)
    else:
        tag = ‘‘‘<a href="%s">%s</a>‘‘‘%(url,direction.name)
        return mark_safe(tag)

@register.simple_tag
def three_catagory_tag(catagory,arg_dic):
    """
    等级
    :param catagory:
    :param arg_dic:
    :return:
    """
    url = reverse("table_video2",kwargs={"dr_id":arg_dic.get("dr_id"),"level_id":arg_dic.get("level_id"),"category_id":catagory.id})
    if str(catagory.id) == arg_dic.get("category_id"):

        tag = ‘‘‘<a href="%s" style="color:red">%s</a>‘‘‘%(url,catagory.name)
        return mark_safe(tag)
    else:
        tag = ‘‘‘<a href="%s">%s</a>‘‘‘%(url,catagory.name)
        return mark_safe(tag)


@register.simple_tag
def three_level_tag(level,arg_dic):
    url = reverse("table_video2",kwargs={"dr_id":arg_dic.get("dr_id"),"level_id":level.id,"category_id":arg_dic.get("category_id")})

    if str(level.id) == arg_dic.get("level_id"):
        tag = ‘‘‘<a href="%s" style="color:red">%s</a>‘‘‘%(url,level.name)
        return mark_safe(tag)
    else:
        tag = ‘‘‘<a href="%s">%s</a>‘‘‘%(url,level.name)
        return mark_safe(tag)

@register.simple_tag
def three_tag_all(arg_dic,key):
    if key == "dr_id":
        url = reverse("table_video2", kwargs={"dr_id":0,"level_id": arg_dic.get("level_id"), "category_id": arg_dic.get("category_id")})
    elif key == "category_id":#category_id
        url = reverse("table_video2", kwargs={"dr_id":arg_dic.get("dr_id"),"level_id": arg_dic.get("level_id"), "category_id":0})
    elif key == "level_id":
        url = reverse("table_video2", kwargs={"dr_id":arg_dic.get("dr_id"),"level_id":0 , "category_id":arg_dic.get("category_id")})
    else:
        url=""
    if arg_dic.get(key) == "0":
        tag = ‘‘‘<a href="%s" style="color:red">全部</a>‘‘‘%(url,)
    else:
        tag = ‘‘‘<a href="%s">全部</a>‘‘‘ % (url,)

    return mark_safe(tag)
#video_tags.py
#三个条件

from django.urls import reverse
from django.utils.safestring import mark_safe
@register.simple_tag
def direction_tag(direction,arg_dic):
    """
    方向
    :param catagory:
    :param arg_dic:
    :return:
    """
    url = reverse("table_video2",kwargs={"dr_id":direction.id,"level_id":arg_dic.get("level_id"),"category_id":arg_dic.get("category_id")})
    if str(direction.id) == arg_dic.get("dr_id"):

        tag = ‘‘‘<a href="%s" style="color:red">%s</a>‘‘‘%(url,direction.name)
        return mark_safe(tag)
    else:
        tag = ‘‘‘<a href="%s">%s</a>‘‘‘%(url,direction.name)
        return mark_safe(tag)

@register.simple_tag
def three_catagory_tag(catagory,arg_dic):
    """
    等级
    :param catagory:
    :param arg_dic:
    :return:
    """
    url = reverse("table_video2",kwargs={"dr_id":arg_dic.get("dr_id"),"level_id":arg_dic.get("level_id"),"category_id":catagory.id})
    if str(catagory.id) == arg_dic.get("category_id"):

        tag = ‘‘‘<a href="%s" style="color:red">%s</a>‘‘‘%(url,catagory.name)
        return mark_safe(tag)
    else:
        tag = ‘‘‘<a href="%s">%s</a>‘‘‘%(url,catagory.name)
        return mark_safe(tag)


@register.simple_tag
def three_level_tag(level,arg_dic):
    url = reverse("table_video2",kwargs={"dr_id":arg_dic.get("dr_id"),"level_id":level.id,"category_id":arg_dic.get("category_id")})

    if str(level.id) == arg_dic.get("level_id"):
        tag = ‘‘‘<a href="%s" style="color:red">%s</a>‘‘‘%(url,level.name)
        return mark_safe(tag)
    else:
        tag = ‘‘‘<a href="%s">%s</a>‘‘‘%(url,level.name)
        return mark_safe(tag)

@register.simple_tag
def three_tag_all(arg_dic,key):
    if key == "dr_id":
        url = reverse("table_video2", kwargs={"dr_id":0,"level_id": arg_dic.get("level_id"), "category_id": arg_dic.get("category_id")})
    elif key == "category_id":#category_id
        url = reverse("table_video2", kwargs={"dr_id":arg_dic.get("dr_id"),"level_id": arg_dic.get("level_id"), "category_id":0})
    elif key == "level_id":
        url = reverse("table_video2", kwargs={"dr_id":arg_dic.get("dr_id"),"level_id":0 , "category_id":arg_dic.get("category_id")})
    else:
        url=""
    if arg_dic.get(key) == "0":
        tag = ‘‘‘<a href="%s" style="color:red">全部</a>‘‘‘%(url,)
    else:
        tag = ‘‘‘<a href="%s">全部</a>‘‘‘ % (url,)

    return mark_safe(tag)
#video2.html
<!DOCTYPE html>
<html lang="en">
{% load video_tags %}
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>筛选条件</h1>
    <div class="filtered">
        <div>
            {% three_tag_all arg_list "dr_id" %}
            {% for direction in direction_list%}
                {% direction_tag direction arg_list %}
            {% endfor%}
        </div>
        <div>
            {% three_tag_all arg_list "category_id" %}
            {% for category in category_list%}
                {% three_catagory_tag category arg_list %}
            {% endfor %}
        </div>

        <div>
            {% three_tag_all arg_list "level_id" %}
            {% for level in level_list%}
                {% three_level_tag level arg_list %}
            {% endfor%}
        </div>

    </div>
    <h1>查询内容</h1>
    <div class="container">
        {%for result in results%}
        <div style="width: 100px;height: 260px;float: left">
            <img src="{{result.img}}" style="width: 100px;height: 150px">
            <p>{{result.title}}</p>
            <p>{{result.summary}}</p>
        </div>
        {%endfor%}
    </div>

</body>
</html>

后来创建的表:

class Level(models.Model):
    """等级"""
    name = models.CharField(max_length=32)

class Direction(models.Model):
    """方向"""
    name = models.CharField(max_length=32)
    d_2_c = models.ManyToManyField("Category")

class Category(models.Model):
    """分类"""
    name = models.CharField(max_length=32)

class Video(models.Model):
    """视频"""
    level = models.ForeignKey("Level",on_delete=models.CASCADE)
    category = models.ForeignKey("Category",on_delete=models.CASCADE)
    title = models.CharField(verbose_name="标题",max_length=32)
    summary = models.CharField(verbose_name="简介",max_length=32)
    img = models.ImageField(verbose_name="图片",upload_to="D:\soft_install\python3\python3.7\PerfectCRM\Perfectcrm\statics\img\Video")
    href = models.CharField(verbose_name="视频地址",max_length=256)
    create_date = models.DateTimeField(auto_now_add=True)

 

三个方向组合的筛选

标签:float   url   sum   end   code   rgs   通过   col   summary   

原文地址:https://www.cnblogs.com/venvive/p/11521773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!