inclusion_tag use of django

1. Create a templatetags in the app already registered folder

2. Create a py file in templatetags

Case

from django.template import Library
from wed import models

register = Library()


@register.inclusion_tag('inclusion/all_project_list.html')
def all_project_list(request):

    my_project_list = models.Project.objects.filter(creator=request.tracer.user)
    join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)

    return {'my': my_project_list, 'join': join_project_list, 'request':request}
templaytetags/project.py

3. Create a html file path that complies with the requirements of step 2

4. Write logic html file

Case

<li class="dropdown active">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
       aria-expanded="false">项目
    </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="#">{{ 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="#">{{ item.project.name }}</a></li>
            {% endfor %}
            <li role="separator" class="divider"></li>
        {% endif %}
        <li><a href="{% url 'project_list' %}" > All items </a> </ li> 
    </ ul> 
</ li>
all_project_list.html

5. In the master

{% load project %}

{% all_project_list request %}

 

Guess you like

Origin www.cnblogs.com/a438842265/p/12567657.html