inclusion_tag 组件

Posted hude

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了inclusion_tag 组件相关的知识,希望对你有一定的参考价值。

技术图片
{% for item in data %}
    <li {% if item.class %} class="{{ item.class }}" {% endif %}>

        <a href="{{ item.url }}" > {{ item.name }} </a>
    </li>
{% endfor %}
menu_list.html
技术图片
    <li class="dropdown active">
        <a href="#" class="active dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
           aria-expanded="false">项目{% if request.tracer.project %}({{ request.tracer.project.name }}){% endif %} <span class="caret"></span></a>


        <ul class="dropdown-menu">
            {% if my %}
                <li><i class="fa fa-list" aria-hidden="true"></i> 我创建的项目</li>
                {% for item in my %}
                    <li><a href="{% url "dashboard" project_id=item.id %}">{{ item.name }}</a></li >
                {% endfor %}

                <li role="separator" class="divider"></li>
            {% endif %}
            {% if join %}
                <li><i class="fa fa-handshake-o" aria-hidden="true"></i> 我参与的项目</li>

                {% for item in join %}
                    <li><a href="{% url "dashboard" project_id=item.project.id %}">{{ item.project.name }}</a></li >
                {% endfor %}
                <li role="separator" class="divider"></li>
            {% endif %}

        <li><a href="{% url "project_list" %}">所有项目</a></li >

        </ul>
    </li>
all_project_list.html
技术图片
from django import template
from web import models
from django.urls import reverse

register = template.Library()

@register.inclusion_tag("inclusion/all_project_list.html")
def all_project_list(request):
    user_obj = request.tracer.user
    # 获取项目列表
    my_project = models.Project.objects.filter(creater=user_obj).all()

    join_project = models.ProjectUser.objects.filter(user=user_obj).all()

    return {"my":my_project,"join":join_project,"request":request}

@register.inclusion_tag("inclusion/menu_list.html")
def menu_list(request):
    project_id = request.tracer.project.id
    data = [
        {"name":"概览","url":reverse("dashboard",kwargs={"project_id":project_id})},
        {"name":"wiki","url":reverse("wiki",kwargs={"project_id":project_id})},
        {"name":"统计","url":reverse("statistics",kwargs={"project_id":project_id})},
        {"name":"问题","url":reverse("issues",kwargs={"project_id":project_id})},
        {"name":"文件","url":reverse("file",kwargs={"project_id":project_id})},
        {"name":"设置","url":reverse("setting",kwargs={"project_id":project_id})},
    ]

    #如果选中该url,则添加类active
    for item in data:
        if request.path_info.startswith(item.get("url")):
            item["class"]="active"

    return {"data":data}
project.py

 

{% load project %}

{% all_project_list request %} {% menu_list request %}

 

以上是关于inclusion_tag 组件的主要内容,如果未能解决你的问题,请参考以下文章

Django中的tags,母版和继承,组件,静态文件相关,自定义simpletag和自定义inclusion_tag

inclusion_tag的运用

Django模板渲染之自定义inclusion_tag详细使用

inclusion_tag 基本使用

django——inclusion_tag

VsCode 代码片段-提升研发效率