VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • Django多条件筛选查询(2)

class="input-group mb-12"> {% with filter_event_form.user as filter_fields %} <div class="input-group-prepend"> <label class="input-group-text {% if filter_fields.errors %}bg-danger {% endif %}" for="{{ filter_fields.id_for_label }}">{{ filter_fields.label }}</label> </div> <select class="form-control col-sm-12" name="user" id="{{ filter_fields.id_for_label }}"> <option>{% for select in filter_fields %}{{ select }}{% endfor %}</option> </select> {% endwith %} </div> </div> <div class="col-2"> <div class="input-group mb-12"> {% with filter_event_form.status as filter_fields %} <div class="input-group-prepend"> <label class="input-group-text {% if filter_fields.errors %}bg-danger{% endif %}" for="{{ filter_fields.id_for_label }}">{{ filter_fields.label }}</label> </div> <select class="form-control col-sm-12" name="status" id="{{ filter_fields.id_for_label }}"> <option>{% for select in filter_fields %}{{ select }}{% endfor %}</option> </select> {% endwith %} </div> </div> <div class="col-2"> <div class="input-group mb-12"> {% with filter_event_form.project as filter_fields %} <div class="input-group-prepend"> <label class="input-group-text {% if filter_fields.errors %}bg-danger {% endif %}" for="{{ filter_fields.id_for_label }}">{{ filter_fields.label }}</label> </div> <select class="form-control col-sm-12" name="project" id="{{ filter_fields.id_for_label }}"> <option>{% for select in filter_fields %}{{ select }}{% endfor %}</option> </select> {% endwith %} </div> </div> <div class="col-2"> <div class="input-group mb-12"> {% with filter_event_form.category as filter_fields %} <div class="input-group-prepend"> <label class="input-group-text {% if filter_fields.errors %}bg-danger {% endif %}" for="{{ filter_fields.id_for_label }}">{{ filter_fields.label }}</label> </div> <select class="form-control col-sm-12" name="category" id="{{ filter_fields.id_for_label }}"> <option>{% for select in filter_fields %}{{ select }}{% endfor %}</option> </select> {% endwith %} </div> </div> <div class="col-2"> <div class="input-group mb-12"> {% with filter_event_form.level as filter_fields %} <div class="input-group-prepend"> <label class="input-group-text {% if filter_fields.errors %}bg-danger {% endif %}" for="{{ filter_fields.id_for_label }}">{{ filter_fields.label }}</label> </div> <select class="form-control col-sm-12" name="level" id="{{ filter_fields.id_for_label }}"> <option>{% for select in filter_fields %}{{ select }}{% endfor %}</option> </select> {% endwith %} </div> </div> <button type="submit" class="btn btn-primary">筛选事件</button> </div> {% csrf_token %} </form> </div> {% else %} <div class="card-body d-flex p-0"> <h3 class="card-title p-3">用户</h3> <ul class="nav nav-pills p-2"> {% active_all request.path 1 %} {% for user_item in user_list %} {% active request.path user_item 1 %} {% endfor %} </ul> </div> <div class="card-body d-flex p-0"> <h3 class="card-title p-3">状态</h3> <ul class="nav nav-pills p-2"> {% active_all request.path 2 %} {% for status_item in status_list %} {% active request.path status_item 2 %} {% endfor %} </ul> </div> <div class="card-body d-flex p-0"> <h3 class="card-title p-3">级别</h3> <ul class="nav nav-pills p-2"> {% active_all request.path 3 %} {% for level_item in level_list %} {% active request.path level_item 3 %} {% endfor %} </ul> </div> <div class="card-body d-flex p-0"> <h3 class="card-title p-3">分类</h3> <ul class="nav nav-pills p-2"> {% active_all request.path 4 %} {% for category_item in category_list %} {% active request.path category_item 4 %} {% endfor %} </ul> </div> <div class="card-body d-flex p-0"> <h3 class="card-title p-3">项目</h3> <ul class="nav nav-pills p-2"> {% active_all request.path 5 %} {% for project_item in project_list %} {% active request.path project_item 5 %} {% endfor %} </ul> </div> {% endif %}

链接生成模板标签

使用模板标签,在应用下创建templatetags的python包,然后创建active.py文件,需要在模板中通过{% load active %}引入模板标签。

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


register = template.Library()


@register.simple_tag
def active_all(request_path, index):
    url_part_list = request_path.split('-')
    # print(url_part_list)
    # ['/event', '0', '0', '0', '0', '0.html']
    # 第五组带.html,需要分开判断

    if url_part_list[index] == '0' or url_part_list[index] == '0.html':
        temp = '''
        <li class="nav-item">
            <a class="nav-link active" href="{href}">全部</a>
        </li>
        '''
    else:
        temp = '''
        <li class="nav-item">
            <a class="nav-link" href="{href}">全部</a>
        </li>
        '''

    if index != 5:
        url_part_list[index] = '0'
    else:
        url_part_list[index] = '0.html'

    href = '-'.join(url_part_list)