Django:如何获取正在呈现的模板的名称

时间:2022-09-04 12:33:44

I'm implementing a bootstrap navbar as show in this example here

我正在这个例子中显示一个bootstrap导航栏

Items in a navbar are <li>'s , the "selected" item has the attribute class="active":

导航栏中的项目为

  • ,“selected”项目的属性为class =“active”:

  • ,“selected”项目的属性为class =“active”:
  •   <li class="active"> <a href="#"> Link1 </a> </li>
      <li>                <a href="#"> Link2 </a> </li>
    

    In Django these items will be within a template, which gets included by any templates that are supposed to display the navbar. I'm thinking about doing it this way:

    在Django中,这些项目将位于模板中,该模板将包含任何应显示导航栏的模板。我正在考虑这样做:

    <li> <a href="/"        class="{% if template_name == "home.djhtml"    %}active{% endif %}"> Home    </a> </li>
    <li> <a href="about/"   class="{% if template_name == "about.djhtml"   %}active{% endif %}"> About   </a> </li>
    <li> <a href="contact/" class="{% if template_name == "contact.djhtml" %}active{% endif %}"> Contact </a> </li>
    

    I would like to know if there is a built-in way to get the template_name (that is, the template being rendered, as passed to render_to_response(), in views.py)

    我想知道是否有一种内置的方法来获取template_name(即,在views.py中传递给render_to_response()的模板)

    Sure, I could explicitly add a template_name variable to render_to_response(), which would solve the problem. But thinking about DRY I feel this shouldn't be needed.

    当然,我可以显式地将template_name变量添加到render_to_response(),这将解决问题。但考虑到DRY,我觉得不应该这样做。

    4 个解决方案

    #1


    14  

    I usually use a custom template tag for this use case of adding a class to the active tab, menu item, etc.

    我通常使用自定义模板标记来为活动选项卡,菜单项等添加类的用例。

    @register.simple_tag
    def active_page(request, view_name):
        from django.core.urlresolvers import resolve, Resolver404
        if not request:
            return ""
        try:
            return "active" if resolve(request.path_info).url_name == view_name else ""
        except Resolver404:
            return ""
    

    And here's a snippet from the top nav:

    这是来自*导航的片段:

    <ul class="nav">
        <li class="{% active_page request "about" %}"><a href="{% url "about" %}">About</a></li>
        ...
    </ul>
    

    #2


    3  

    I use:

    我用:

    class="{% if 'about' in request.path %}active{% endif %}"
    

    It's a little shorter and more robust if the URI changes, just watch out if more than one path uses about.

    如果URI发生变化,它会更短,更强大,只需注意是否有多条路径使用。

    #3


    2  

    There is a much faster way, without creating any custom template tag!

    有一种更快的方法,无需创建任何自定义模板标签!

    <ul class = 'nav'>
        <li class="{% ifequal request.path 'about/'%} active {% endifequal%}">
            <a href="{% url "about" %}">About</a>
        </li>
    </ul>
    

    just be aware about request.path. It might be with slash symbol at the beginning or without at the end of the path!

    请注意request.path。它可能是在开头的斜线符号或没有在路径的末尾!

    #4


    0  

    Appending prog.Dusans answer for Django 1.7

    为Django 1.7添加prog.Dusans答案

    settings.py
    TEMPLATE_CONTEXT_PROCESSORS = (
        'django.core.context_processors.request',
        'django.contrib.auth.context_processors.auth'
    )
    
    views.py
    from django.shortcuts import render
    
    def index(request):
        return render(request, 'index.html')
    
    template
    {% ifequal request.path '/pathname'%}active{% endifequal%}
    

    Best is to add it to the base template so you need only to do it once.

    最好是将其添加到基本模板,因此您只需要执行一次。

    Like I this version more than Kevin Stones because you have to add almost equal code to the template and don't need a template tag after all.

    就像我这个版本比Kevin Stones更多,因为你必须为模板添加几乎相同的代码,毕竟不需要模板标签。

    #1


    14  

    I usually use a custom template tag for this use case of adding a class to the active tab, menu item, etc.

    我通常使用自定义模板标记来为活动选项卡,菜单项等添加类的用例。

    @register.simple_tag
    def active_page(request, view_name):
        from django.core.urlresolvers import resolve, Resolver404
        if not request:
            return ""
        try:
            return "active" if resolve(request.path_info).url_name == view_name else ""
        except Resolver404:
            return ""
    

    And here's a snippet from the top nav:

    这是来自*导航的片段:

    <ul class="nav">
        <li class="{% active_page request "about" %}"><a href="{% url "about" %}">About</a></li>
        ...
    </ul>
    

    #2


    3  

    I use:

    我用:

    class="{% if 'about' in request.path %}active{% endif %}"
    

    It's a little shorter and more robust if the URI changes, just watch out if more than one path uses about.

    如果URI发生变化,它会更短,更强大,只需注意是否有多条路径使用。

    #3


    2  

    There is a much faster way, without creating any custom template tag!

    有一种更快的方法,无需创建任何自定义模板标签!

    <ul class = 'nav'>
        <li class="{% ifequal request.path 'about/'%} active {% endifequal%}">
            <a href="{% url "about" %}">About</a>
        </li>
    </ul>
    

    just be aware about request.path. It might be with slash symbol at the beginning or without at the end of the path!

    请注意request.path。它可能是在开头的斜线符号或没有在路径的末尾!

    #4


    0  

    Appending prog.Dusans answer for Django 1.7

    为Django 1.7添加prog.Dusans答案

    settings.py
    TEMPLATE_CONTEXT_PROCESSORS = (
        'django.core.context_processors.request',
        'django.contrib.auth.context_processors.auth'
    )
    
    views.py
    from django.shortcuts import render
    
    def index(request):
        return render(request, 'index.html')
    
    template
    {% ifequal request.path '/pathname'%}active{% endifequal%}
    

    Best is to add it to the base template so you need only to do it once.

    最好是将其添加到基本模板,因此您只需要执行一次。

    Like I this version more than Kevin Stones because you have to add almost equal code to the template and don't need a template tag after all.

    就像我这个版本比Kevin Stones更多,因为你必须为模板添加几乎相同的代码,毕竟不需要模板标签。