1. 程式人生 > >組合搜索框的實現

組合搜索框的實現

ive AR htm djang emp ID ict 實現 safe

在當前APP目錄下新建templatetags目錄:

新建任意名字python名字,然後前端裏面的寫法是現在html頁面開頭導入:

{% load filter %}
 <div class="cotent-head1"><i class="fa fa-search" aria-hidden="true"></i>搜索條件</div>
    <div class="cotent-searh" >
        <ul class="nav nav-pills">
        {% filter_all argdict "article_type_id" %}

        {% filterother argdict ‘article_type_id‘ type_choice_list %}

        </ul>
    </div>
    <div class="cotent-searh" >
        <ul class="nav nav-pills">
        {% filter_all argdict "category_id" %}

        {% filterother argdict ‘category_id‘ blogobj.category.all %}

        </ul>
    </div>
    <div class="cotent-searh" >
        <ul class="nav nav-pills">
        {% filter_all argdict "tags__nid" %}

        {% filterother argdict ‘tags__nid‘ blogobj.tag.all %}

        </ul>
    </div>

  

filter文件內容

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

register = template.Library()


@register.simple_tag
def filter_all(argdict,typeid):
    TYPE_LIST = {‘article_type_id‘:argdict[‘article_type_id‘],
                 ‘category_id‘:argdict[‘category_id‘],
                 ‘tags__nid‘:argdict[‘tags__nid‘],
                 }
    for row in TYPE_LIST.keys():
        if row == typeid:
            TYPE_LIST[row] = 0
    TYPE_LIST = tuple(TYPE_LIST.values())
    if argdict[typeid] == 0:
        ret = ‘<li role="presentation" class="active"><a href="/backend/article-%s-%s-%s-0.html">全部</a></li>‘%TYPE_LIST
    else:
        ret = ‘<li role="presentation" ><a href="/backend/article-%s-%s-%s-0.html">全部</a></li>‘%TYPE_LIST
    return mark_safe(ret)

@register.simple_tag
def filterother(argdict,typeid,typelist):
    TYPE_DICT= {‘article_type_id‘: argdict[‘article_type_id‘],
                 ‘category_id‘: argdict[‘category_id‘],
                 ‘tags__nid‘: argdict[‘tags__nid‘],
                 }
    type_list=[]
    for row in typelist:
        try:
            type_id= row[‘nid‘]
        except Exception:
            type_id = row.nid

        for i in TYPE_DICT.keys():
            if i == typeid:
                    TYPE_DICT[i] = type_id
        list1 = list(TYPE_DICT.values())
        try:
             str1 = row[‘title‘]
        except Exception:
            str1 = row.title
        list1.append(str1)
        TYPE_LIST = tuple(list1)
        if argdict[typeid] == type_id:
            ret = ‘‘‘<li role="presentation" class="item active" >
            <a href="/backend/article-%s-%s-%s-0.html">
            %s</a></li>‘‘‘%TYPE_LIST
        else:
            ret = ‘‘‘<li role="presentation" class="item" >
                        <a href="/backend/article-%s-%s-%s-0.html">
                        %s</a></li>‘‘‘ % TYPE_LIST
        type_list.append(ret)
    type_str = ‘‘.join(type_list)
    return mark_safe(type_str)

  

組合搜索框的實現