【Python之路】特别篇--组合搜索功能实现

组合搜索:

  根据特定标签内容,返回符合的数据。

效果图:

 

设计规划:

一、数据库表划分:

1.方向表,(运维自动化,Python开发,。。)

2.分类表,(Python,Java,C#,。)

3.多对多,方向与分类外键表,

4.视频表,(视频1,视频2,。。。)

二、表间关系:

方向表与分类表:为多对多关系,

1个方向可以有多种分类,1个分类可以同时对应多个方向。

分类表与视频表:一对多关系,

1个分类可以有多个视频。

三、表内详细设计:

方向表:

class Direction(models.Model):
    weight = models.IntegerField(verbose_name='权重(按从大到小排列)', default=0)
    name = models.CharField(verbose_name='名称', max_length=32)
    classification = models.ManyToManyField('Classification')

    class Meta:
        db_table = 'Direction'
        verbose_name_plural = u'方向(视频方向)'
    def __str__(self):
        return self.name

classfication为方向表(Direction)多对多关系字段;

分类表:

class Classification(models.Model):
    weight = models.IntegerField(verbose_name='权重(按从大到小排列)', default=0)
    name = models.CharField(verbose_name='名称', max_length=32)

    class Meta:
        db_table = 'Classification'
        verbose_name_plural = u'分类(视频分类)'

    def __str__(self):
        return self.name
View Code

视频表:

class Video(models.Model):

    status_choice = (
        (0, u'下线'),
        (1, u'上线'),
    )
    level_choice = (
        (1, u'初级'),
        (2, u'中级'),
        (3, u'高级'),
    )
    status = models.IntegerField(verbose_name='状态', choices=status_choice, default=1)
    level = models.IntegerField(verbose_name='级别', choices=level_choice, default=1)
    classification = models.ForeignKey('Classification', null=True, blank=True)

    weight = models.IntegerField(verbose_name='权重(按从大到小排列)', default=0)

    title = models.CharField(verbose_name='标题', max_length=32)
    summary = models.CharField(verbose_name='简介', max_length=32)
    img = models.ImageField(verbose_name='图片', upload_to='./static/images/Video/')
    href = models.CharField(verbose_name='视频地址', max_length=256)

    create_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'Video'
        verbose_name_plural = u'视频'

    def __str__(self):
        return self.title
View Code

level为等级字段,status为视频状态字段;

 

具体效果:

通过对比其他同类型的搜索示例发现,他们大多数都通过URL地址来向后台传递类别 id 查询数据库,然后返回数据给前端页面。

可以发现:方向对应第一个数字,分类对应第二个数字,等级对应第三个数字。

方向的标签代码为:

当点击分类或等级时,其自身对应的id数字会发生改变,而另外两个数字id不产生变化。

 

设计步骤:

1.我们先设计出通过输入网页URL地址的方式,动态改变页面标签代码中的值。

设计URL选择器,正则表达式:

url(r'^video-(?P<direction_id>\d+)-(?P<classfication_id>\d+)-(?P<level_id>\d+).html', views.video),

direction_id 为获得方向id,classfication_id为获得分类id,level_id为获得等级id

2.输入url: /video-1-2-1.html 后参数传递给 views.video()函数处理。

def video(request,*args,**kwargs):
    current_url = request.path     #当前输入网页路径 /video-0-0-0.html
    direction_id = int(kwargs.get('direction_id'))  #获得相应的id
    classfication_id = int(kwargs.get('classfication_id'))
    level_id = int(kwargs.get('level_id'))

接下来通过获得的id数据查询数据库信息;

此时,我们得知数据库中id字段是从1开始递增,所以我们设计当id=0 时为获得全部数据。

1.当方向为0时,表示直接从数据库获取所有的分类信息:

由于,方向字段在网页上表现为永远显示全部数据。所以设计如下

def video(request,*args,**kwargs):
    # ...... 省略了部分
    # 获得所有方向信息:
    direction_list = models.Direction.objects.all().values('name','id')
    if direction_id != 0 :
        # 分类不等于0 即选择了其中一个分类, /video-1-3-1.html
        # 取 url相对应的分类信息(根据 方向id 进行查询,连表查询)
        direction = models.Direction.objects.get(id=direction_id)
        classfication_list = direction.classification.values('name', 'id')
    else:
        classfication_list = models.Classification.objects.all().values('name','id')    #分类等于0 为 全部, 直接取全部分类数据
    # 获得所有等级信息 
    # level_list = models.Video.level_choice
    ret = map(lambda x:{'name':x[1],'id':x[0]},models.Video.level_choice)
    level_list = list(ret)
    # 数据返回前端页面
    return render(request,'video.html',{'direction_list':direction_list,'class_list':classfication_list,'level_list':level_list,'current_url':current_url})

其中,等级信息,存放在video表中的level字段中,以元组形式存放,为了统一我们将(元组,..)的形式 => [字典,..],使用了map()函数

向前端页面返回了,方向,分类,等级的数据和当前url地址。 /video-1-3-1.html。

核心:前端接收到当前url地址后,通过模版语言simple_tag,动态的构造a标签的href属性,即可表现为当改变url时,标签href也同时发生改变

前端页面:

<div>
    {% for item in direction_list %}
        {% simple_tag函数 current_url item %}
    {% endfor %}
</div>

3.设计simple_tag函数。

需要在项目app下创建 templatetags包, 然后自定义一个文件 xx.py

from django import template
from django.utils.safestring import mark_safe

register = template.Library()

@register.simple_tag
def action1(current_url, item):

    url_part_list = current_url.split('-')

    # 如果当前url中第一个位置 video-2-0-0.html
    if str(item['id']) == url_part_list[1]:
        temp = "<a href='%s' class='active'>%s</a>"
    else:
        temp = "<a href='%s'>%s</a>"

    url_part_list[1] = str(item['id'])

    ur_str = '-'.join(url_part_list)
    temp = temp %(ur_str, item['name'])
    return mark_safe(temp)

得到了一个处理 方向 标签的模板函数,方向、等级标签的处理方式同上,只是url切割的位置稍有不同!

{% load newtag %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        a{
            display: inline-block;
            padding: 8px;
        }
        .active{
            background-color: coral;
            color: white;
        }
    </style>
</head>
<body>
    <h3>选择:</h3>
    <div>
        {% for item in direction_list %}

             {% action1 current_url item %}
        {% endfor %}
    </div>
    <div>

        {% for item in class_list %}

            {% action2 current_url item %}
        {% endfor %}
    </div>
    <div>

        {% for item in level_list %}
            {% action3 current_url item %}
        {% endfor %}
    </div>
    <hr />
    <div>

    </div>
    <h3>视频:</h3>
    <hr />

</body>
</html>
前端video.html
from django import template
from django.utils.safestring import mark_safe

register = template.Library()

@register.simple_tag
def action1(current_url, item):
    # videos-0-0-1.html
    # item: id name
    # video-   2   -0-0.html
    url_part_list = current_url.split('-')

    # 如果当前url中第一个位置 video-2-0-0.html
    if str(item['id']) == url_part_list[1]:
        temp = "<a href='%s' class='active'>%s</a>"
    else:
        temp = "<a href='%s'>%s</a>"

    url_part_list[1] = str(item['id'])

    ur_str = '-'.join(url_part_list)
    temp = temp %(ur_str, item['name'])
    return mark_safe(temp)
@register.simple_tag
def action2(current_url, item):
    # videos-0-0-1.html
    # item: id name
    url_part_list = current_url.split('-')

    # 如果当前url中第一个位置 video-2-0-0.html
    if str(item['id']) == url_part_list[2]:
        temp = "<a href='%s' class='active'>%s</a>"
    else:
        temp = "<a href='%s'>%s</a>"


    url_part_list[2] = str(item['id'])
    ur_str = '-'.join(url_part_list)
    temp = temp %(ur_str, item['name'])
    return mark_safe(temp)

@register.simple_tag
def action3(current_url, item):
    # videos-0-0-1.html
    # item: id name
    url_part_list = current_url.split('-')
    # 如果当前url中第三个位置 video-2-0-0.html
    if str(item['id']) == url_part_list[3].split('.')[0]:
        temp = "<a href='%s' class='active'>%s</a>"
    else:
        temp = "<a href='%s'>%s</a>"

    url_part_list[3] = str(item['id']) + '.html'

    ur_str = '-'.join(url_part_list)
    temp = temp %(ur_str, item['name'])
    return mark_safe(temp)
newtag.py

然后再设计一个在前端同时输出, "全部" 的simple_tag函数

{% load newtag %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        a{
            display: inline-block;
            padding: 8px;
        }
        .active{
            background-color: coral;
            color: white;
        }
    </style>
</head>
<body>
    <h3>选择:</h3>
    <div>
        {% action_all current_url 1 %} :
        {% for item in direction_list %}

             {% action1 current_url item %}
        {% endfor %}
    </div>
    <div>
        {% action_all current_url 2 %} :
        {% for item in class_list %}

            {% action2 current_url item %}
        {% endfor %}
    </div>
    <div>
        {% action_all current_url 3 %} :
        {% for item in level_list %}
            {% action3 current_url item %}
        {% endfor %}
    </div>
    <hr />
    <div>

    </div>
    <h3>视频:</h3>
    <hr />

</body>
</html>
前端video.html
@register.simple_tag
def action_all_1(current_url):
    """
    获取当前url,video-1-1-2.html
    :param current_url:
    :param item:
    :return:
    """
    url_part_list = current_url.split('-')
    if url_part_list[1] == "0":
        temp = "<a href='%s' class='active'>全部</a>"
    else:
        temp = "<a href='%s'>全部</a>"


    url_part_list[1] = "0"
    #  video 0 1 2.html
    href = '-'.join(url_part_list)

    temp = temp % (href,)
    return mark_safe(temp)

@register.simple_tag
def action_all_2(current_url):
    """
    获取当前url,video-1-1-2.html
    :param current_url:
    :param item:
    :return:
    """
    url_part_list = current_url.split('-')

    if url_part_list[2] == "0":
        temp = "<a href='%s' class='active'>全部</a>"
    else:
        temp = "<a href='%s'>全部</a>"

    url_part_list[2] = "0"
    #  video 0 1 2.html
    href = '-'.join(url_part_list)

    temp = temp % (href,)
    return mark_safe(temp)

@register.simple_tag
def action_all_3(current_url):
    """
    获取当前url,video-1-1-2.html
    :param current_url:
    :param item:
    :return:
    """
    url_part_list = current_url.split('-')
    #  video 0 1 2.html
    if url_part_list[3] == "0.html":
        temp = "<a href='%s' class='active'>全部</a>"
    else:
        temp = "<a href='%s'>全部</a>"

    url_part_list[3] = "0.html"

    href = '-'.join(url_part_list)

    temp = temp % (href,)
    return mark_safe(temp)
newtag.py

为了降低simple_tag代码过于冗余,我们用if..else统一归类成一个函数:

{% load newtag %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        a{
            display: inline-block;
            padding: 8px;
            color: black;
            text-decoration: none;
        }
        .alt{
            color: black;
            font-weight: bolder;
        }
        .active{
            background-color: coral;
            color: white;
        }
        .item-list{
            margin: 6px;
        }
        .video-list{
            display: inline-block;
            margin: 6px;
        }
        .video-list img{
            height: 160px;
            width: 250px;
            border:0 ;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <h3>选择:</h3>
    <div class="item-list">
        <a href="javascript:;" class="alt">方向 :</a>
        {% action_all current_url 1 %}
        {% for item in direction_list %}
            {% action current_url item 1 %}
        {% endfor %}
    </div>
    <div class="item-list">
        <a href="javascript:;" class="alt">分类 :</a>
        {% action_all current_url 2 %}
        {% for item in class_list %}
            {% action current_url item 2 %}
        {% endfor %}
    </div>
    <div class="item-list">
        <a href="javascript:;" class="alt">等级 :</a>
        {% action_all current_url 3 %}
        {% for item in level_list %}
            {% action current_url item 3 %}
        {% endfor %}
    </div>
    <hr>

    <h3>视频:</h3>
    <hr>

</body>
</html>
前端video.html
from django import template
from django.utils.safestring import mark_safe

register = template.Library()

@register.simple_tag
def action_all(current_url,index):
    url_split = current_url.split('-')
    if index == 3:
        if url_split[index] == '0.html':
            TEMP = '''
                     <a href='%s' class='active'>全部</a>
                '''
        else:
            TEMP = '''
                     <a href='%s'>全部</a>
                '''
        url_split[index] = '0.html'
    else:
        if url_split[index] == '0':
            TEMP = '''
                     <a href='%s' class='active'>全部</a>
                '''
        else:
            TEMP = '''
                     <a href='%s'>全部</a>
                '''
        url_split[index] = '0'
    url_str = '-'.join(url_split)
    TEMP = TEMP % (url_str)

    return mark_safe(TEMP)



@register.simple_tag
def action(current_url,item,index):
    url_split = current_url.split('-')
    if index == 3:
        if url_split[index].split('.')[0] == str(item['id']):
            TEMP = '''
                    <a href='%s' class='active'>%s</a>
                '''
        else:
            TEMP = '''
                    <a href='%s'>%s</a>
                '''
        url_split[index] = str(item['id']) + '.html'
    else:
        if url_split[index] == str(item['id']):
            TEMP = '''
                <a href='%s' class='active'>%s</a>
            '''
        else:
            TEMP = '''
                <a href='%s'>%s</a>
            '''
        url_split[index] = str(item['id'])
    url_str = '-'.join(url_split)
    TEMP = TEMP%(url_str,item['name'])
    return mark_safe(TEMP)
newtag.py

至此,我们已经完成了 页面上输出全部的方向、分类、等级的数据,以及通过url地址改变动态修改了a标签href代码和添加了选中时特效。我们还需要添加限制规则,规定当 方向 改变时,出现不同的分类!

4.进一步考虑添加规则!

当输入的url中,方向id等于0 时,  => 显示全部分类,  video-0-0-2.html

          不等于0 时,=>  if  分类id等于0  =>  分类根据 方向id 取出数据

                        不等于0 =>  分类根据 方向id 取出数据   当先输入的url中,分类id没有出现在通过查询获得的分类id时,需要把分类id 置为0。

def video(request,*args,**kwargs):

    current_url = request.path
    direction_id = int(kwargs.get('direction_id'))
    classfication_id = int(kwargs.get('classfication_id'))
    level_id = int(kwargs.get('level_id'))

    direction_list = models.Direction.objects.all().values('name','id')
    if direction_id == 0 :   # 方向 = 0
        # 分类全部显示
        classfication_list = models.Classification.objects.all().values('name', 'id')
    else:
        # 方向 != 0 , 分类 根据方向,查询
        if classfication_id == 0:
            # 分类为0 ,根据url方向 取全部
            direction = models.Direction.objects.get(id=direction_id)
            classfication_list = direction.classification.values('name', 'id')
            classfication_id_list = list(map(lambda x: x['id'], classfication_list))
        else:
            # 分类不为0 , 根据具体方向,取具体分类
            direction = models.Direction.objects.get(id=direction_id)
            classfication_list = direction.classification.values('name', 'id')
            classfication_id_list = list(map(lambda x: x['id'], classfication_list))

            if classfication_id in classfication_id_list:
                pass
            else:
                url_list = current_url.split('-')
                url_list[2] = '0'
                current_url = '-'.join(url_list)

    # 等级,统一格式,规范化输出
    # level_list = models.Video.level_choice
    ret = map(lambda x:{'name':x[1],'id':x[0]},models.Video.level_choice)
    level_list = list(ret)

    return render(request,'video.html',{'direction_list':direction_list,
                                        'class_list':classfication_list,
                                        'level_list':level_list,
                                        'current_url':current_url,
                                        })
views.video()

5.最后考虑输出视频数据:

当方向、分类、等级id都为0时,输出全部的视频;

当方向=0、分类!=0时,根据分类id查询数据库

当方向!=0、分类=0时,根据分类id查询数据库

当方向!=0、分类!=0时,根据分类id查询数据库

当等级!=0时, 根据分类id,等级id查询数据库                                

def video(request,*args,**kwargs):

    current_url = request.path
    direction_id = int(kwargs.get('direction_id'))
    classfication_id = int(kwargs.get('classfication_id'))
    level_id = int(kwargs.get('level_id'))

    q={}
    q['status'] = 1

    direction_list = models.Direction.objects.all().values('name','id')
    if direction_id == 0 :   # 方向 = 0
        # 分类全部显示
        classfication_list = models.Classification.objects.all().values('name', 'id')
        if classfication_id == 0:
            # 方向全部,分类全部,视频显示全部
            pass
        else:
            # 方向全部,分类部分,视频属于分类下的
            q['classification__in'] = [classfication_id,]
    else:
        # 方向 != 0 , 分类 根据方向,查询
        if classfication_id == 0:
            # 分类为0 ,根据url方向 取全部
            direction = models.Direction.objects.get(id=direction_id)
            classfication_list = direction.classification.values('name', 'id')
            classfication_id_list = list(map(lambda x: x['id'], classfication_list))
            q['classification__in'] = classfication_id_list
        else:
            # 分类不为0 , 根据具体方向,取具体分类下视频
            direction = models.Direction.objects.get(id=direction_id)
            classfication_list = direction.classification.values('name', 'id')
            classfication_id_list = list(map(lambda x: x['id'], classfication_list))
            # 方向部分,分类部分,视频属于分类下的
            q['classification__in'] = [classfication_id,]

            if classfication_id in classfication_id_list:
                pass
            else:
                url_list = current_url.split('-')
                url_list[2] = '0'
                current_url = '-'.join(url_list)

    # 等级,统一格式,规范化输出
    # level_list = models.Video.level_choice
    ret = map(lambda x:{'name':x[1],'id':x[0]},models.Video.level_choice)
    level_list = list(ret)

    # 如果选择了等级 , 获取id , 否则显示全部
    if level_id != 0:
        q['level'] = level_id


    video_list = models.Video.objects.filter(**q).values('title','summary','img','href')

    return render(request,'video.html',{'direction_list':direction_list,
                                        'class_list':classfication_list,
                                        'level_list':level_list,
                                        'current_url':current_url,
                                        'video_list':video_list,
                                        })
views.video()

注意: 查询时,可以通过构造一个字典的方式添加查询条件,返回最终结果。

q = {}
ret = models.X.objects.filter(**q).values('..,..')

 

最终效果图:

完整代码:

from django.shortcuts import render,HttpResponse
import json

# Create your views here.
from app01 import models
def video(request,*args,**kwargs):

    current_url = request.path
    direction_id = int(kwargs.get('direction_id'))
    classfication_id = int(kwargs.get('classfication_id'))
    level_id = int(kwargs.get('level_id'))

    q={}
    q['status'] = 1

    direction_list = models.Direction.objects.all().values('name','id')
    if direction_id == 0 :   # 方向 = 0
        # 分类全部显示
        classfication_list = models.Classification.objects.all().values('name', 'id')
        if classfication_id == 0:
            # 方向全部,分类全部,视频显示全部
            pass
        else:
            # 方向全部,分类部分,视频属于分类下的
            q['classification__in'] = [classfication_id,]
    else:
        # 方向 != 0 , 分类 根据方向,查询
        if classfication_id == 0:
            # 分类为0 ,根据url方向 取全部
            direction = models.Direction.objects.get(id=direction_id)
            classfication_list = direction.classification.values('name', 'id')
            classfication_id_list = list(map(lambda x: x['id'], classfication_list))
            q['classification__in'] = classfication_id_list
        else:
            # 分类不为0 , 根据具体方向,取具体分类下视频
            direction = models.Direction.objects.get(id=direction_id)
            classfication_list = direction.classification.values('name', 'id')
            classfication_id_list = list(map(lambda x: x['id'], classfication_list))
            # 方向部分,分类部分,视频属于分类下的
            q['classification__in'] = [classfication_id,]

            if classfication_id in classfication_id_list:
                pass
            else:
                url_list = current_url.split('-')
                url_list[2] = '0'
                current_url = '-'.join(url_list)

    # 等级,统一格式,规范化输出
    # level_list = models.Video.level_choice
    ret = map(lambda x:{'name':x[1],'id':x[0]},models.Video.level_choice)
    level_list = list(ret)

    # 如果选择了等级 , 获取id , 否则显示全部
    if level_id != 0:
        q['level'] = level_id


    video_list = models.Video.objects.filter(**q).values('title','summary','img','href')

    return render(request,'video.html',{'direction_list':direction_list,
                                        'class_list':classfication_list,
                                        'level_list':level_list,
                                        'current_url':current_url,
                                        'video_list':video_list,
                                        })
后台 views.py
from django.db import models

class Direction(models.Model):
    weight = models.IntegerField(verbose_name='权重(按从大到小排列)', default=0)
    name = models.CharField(verbose_name='名称', max_length=32)

    classification = models.ManyToManyField('Classification')

    class Meta:
        db_table = 'Direction'
        verbose_name_plural = u'方向(视频方向)'

    def __str__(self):
        return self.name


class Classification(models.Model):
    weight = models.IntegerField(verbose_name='权重(按从大到小排列)', default=0)
    name = models.CharField(verbose_name='名称', max_length=32)

    class Meta:
        db_table = 'Classification'
        verbose_name_plural = u'分类(视频分类)'

    def __str__(self):
        return self.name


class Video(models.Model):

    status_choice = (
        (0, u'下线'),
        (1, u'上线'),
    )
    level_choice = (
        (1, u'初级'),
        (2, u'中级'),
        (3, u'高级'),
    )
    status = models.IntegerField(verbose_name='状态', choices=status_choice, default=1)
    level = models.IntegerField(verbose_name='级别', choices=level_choice, default=1)
    classification = models.ForeignKey('Classification', null=True, blank=True)

    weight = models.IntegerField(verbose_name='权重(按从大到小排列)', default=0)

    title = models.CharField(verbose_name='标题', max_length=32)
    summary = models.CharField(verbose_name='简介', max_length=32)
    img = models.ImageField(verbose_name='图片', upload_to='./static/images/Video/')
    href = models.CharField(verbose_name='视频地址', max_length=256)

    create_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'Video'
        verbose_name_plural = u'视频'

    def __str__(self):
        return self.title
后台 models.py
# templatetags --newtag.py
#!/usr/bin/env python
# -*-coding:utf-8 -*-

from django import template
from django.utils.safestring import mark_safe

register = template.Library()

@register.simple_tag
def image_show(item,counter,allcount,remainder):
    '''
    :param item:   当前循环获取的数据
    :param counter:  当前循环次数(模版语言for循环默认从1开始)
    :param allcount:  页面分布列数
    :param remainder: 余数
    :return:
    '''

    TEMP = '''
     <div style="width: 245px;" >
            <img src="/%s" alt="" style="width: 245px;height: 250px">
            <p>%s</p>
            <p>%s</p>
    </div>
    '''
    if counter%allcount == remainder:
        TEMP = TEMP %(item['student__pic'],
                      item['student__name'],
                      item['letter_of_thanks'],
                      )
        return mark_safe(TEMP)
    else:
        return ''

@register.filter
def image_show2(value,arg):

    countet = value
    allcount = int(arg.split(',')[0])
    remainder = int(arg.split(',')[1])
    if countet%allcount == remainder:
        return True
    else:
        return False



@register.simple_tag
def action1(current_url,item):
    url_split = current_url.split('-')
    if url_split[1] == str(item['id']):
        TEMP = '''
            <a href='%s' class='active'>%s</a>
        '''
    else:
        TEMP = '''
            <a href='%s'>%s</a>
        '''
    url_split[1] = str(item['id'])
    url_str = '-'.join(url_split)
    TEMP = TEMP%(url_str,item['name'])

    return mark_safe(TEMP)

@register.simple_tag
def action2(current_url,item):
    url_split = current_url.split('-')
    if url_split[2] == str(item['id']):
        TEMP = '''
                <a href='%s' class='active'>%s</a>
            '''
    else:
        TEMP = '''
                <a href='%s'>%s</a>
            '''
    url_split[2] = str(item['id'])
    url_str = '-'.join(url_split)
    TEMP = TEMP%(url_str,item['name'])

    return mark_safe(TEMP)

@register.simple_tag
def action3(current_url,item):
    url_split = current_url.split('-')
    if url_split[3].split('.')[0] == str(item['id']):
        TEMP = '''
                <a href='%s' class='active'>%s</a>
            '''
    else:
        TEMP = '''
                <a href='%s'>%s</a>
            '''
    url_split[3] = str(item['id']) + '.html'
    url_str = '-'.join(url_split)
    TEMP = TEMP%(url_str,item['name'])

    return mark_safe(TEMP)

@register.simple_tag
def action_all_1(current_url):
    url_split = current_url.split('-')
    if url_split[1] == '0':
        TEMP = '''
                 <a href='%s' class='active'>全部</a>
            '''
    else:
        TEMP = '''
                 <a href='%s'>全部</a>
            '''
    url_split[1] = '0'
    url_str = '-'.join(url_split)
    TEMP = TEMP % (url_str)

    return mark_safe(TEMP)

@register.simple_tag
def action_all_2(current_url):
    url_split = current_url.split('-')
    if url_split[2] == '0':
        TEMP = '''
                 <a href='%s' class='active'>全部</a>
            '''
    else:
        TEMP = '''
                 <a href='%s'>全部</a>
            '''
    url_split[2] = '0'
    url_str = '-'.join(url_split)
    TEMP = TEMP % (url_str)

    return mark_safe(TEMP)

@register.simple_tag
def action_all_3(current_url):
    url_split = current_url.split('-')
    if url_split[3] == '0.html':
        TEMP = '''
                 <a href='%s' class='active'>全部</a>
            '''
    else:
        TEMP = '''
                 <a href='%s'>全部</a>
            '''
    url_split[3] = '0.html'
    url_str = '-'.join(url_split)
    TEMP = TEMP % (url_str)

    return mark_safe(TEMP)

@register.simple_tag
def action_all(current_url,index):
    url_split = current_url.split('-')
    if index == 3:
        if url_split[index] == '0.html':
            TEMP = '''
                     <a href='%s' class='active'>全部</a>
                '''
        else:
            TEMP = '''
                     <a href='%s'>全部</a>
                '''
        url_split[index] = '0.html'
    else:
        if url_split[index] == '0':
            TEMP = '''
                     <a href='%s' class='active'>全部</a>
                '''
        else:
            TEMP = '''
                     <a href='%s'>全部</a>
                '''
        url_split[index] = '0'
    url_str = '-'.join(url_split)
    TEMP = TEMP % (url_str)

    return mark_safe(TEMP)



@register.simple_tag
def action(current_url,item,index):
    url_split = current_url.split('-')
    if index == 3:
        if url_split[index].split('.')[0] == str(item['id']):
            TEMP = '''
                    <a href='%s' class='active'>%s</a>
                '''
        else:
            TEMP = '''
                    <a href='%s'>%s</a>
                '''
        url_split[index] = str(item['id']) + '.html'
    else:
        if url_split[index] == str(item['id']):
            TEMP = '''
                <a href='%s' class='active'>%s</a>
            '''
        else:
            TEMP = '''
                <a href='%s'>%s</a>
            '''
        url_split[index] = str(item['id'])
    url_str = '-'.join(url_split)
    TEMP = TEMP%(url_str,item['name'])
    return mark_safe(TEMP)
后台 newtag.py
{% load newtag %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        a{
            display: inline-block;
            padding: 8px;
            color: black;
            text-decoration: none;
        }
        .alt{
            color: black;
            font-weight: bolder;
        }
        .active{
            background-color: coral;
            color: white;
        }
        .item-list{
            margin: 6px;
        }
        .video-list{
            display: inline-block;
            margin: 6px;
        }
        .video-list img{
            height: 160px;
            width: 250px;
            border:0 ;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <h3>选择:</h3>
    <div class="item-list">
        <a href="javascript:;" class="alt">方向 :</a>
        {% action_all current_url 1 %}
        {% for item in direction_list %}
            {% action current_url item 1 %}
        {% endfor %}
    </div>
    <div class="item-list">
        <a href="javascript:;" class="alt">分类 :</a>
        {% action_all current_url 2 %}
        {% for item in class_list %}
            {% action current_url item 2 %}
        {% endfor %}
    </div>
    <div class="item-list">
        <a href="javascript:;" class="alt">等级 :</a>
        {% action_all current_url 3 %}
        {% for item in level_list %}
            {% action current_url item 3 %}
        {% endfor %}
    </div>
    <hr>

    <h3>视频:</h3>
    {% for item in video_list %}
        <a href="{{ item.href }}" class="video-list">
            <img src="{{ item.img }}" alt="">
            <p>{{ item.title }}</p>
            <p>{{ item.summary }}</p>
        </a>
    {% endfor %}
    <hr>

</body>
</html>
前端 video.html

 

posted @ 2017-04-18 20:18  5_FireFly  阅读(1334)  评论(0)    收藏  举报
web
counter