How to add class active for html templates, eg: nav menus, bassed in generic.ListView. This my problem that was asked in gitter of django, and this is my question of it:

somebody can help me how to add class active for html templates, eg: nav menus, bassed in generic.ListView?
some idea i was try with {% if request.resolver_match.url_name == "name" %} and {% if request.get_full_path == '/mypage/' %} but it isn’t work.. i think may it because pagination in generic.ListView
any idea how to solved it?

Yup, thereis my problem because i work with generic.ListView, (or similiar with view that ^use list view). 

And was answered with nick pace-noge there (, he assumming to use templatetags. I never think it before.. and Thank you so much.. angel

And thereis answer of it:

  1. in your app/
url(r'^direktorat/$', DirektoratListView.as_view(), name="direktorat-list"),
  1. in your templatetags, app/templatetags/
from django import template

register = template.Library()

def active(request, pattern):
    import re
    if, request.path):
        return "active"
    return ""
  1. in your templates
{% load active_nav %} # load the templatetags
{% url 'direktorat-list' as direktorat_list_url %}  #save it as variable for future change

<!-- link -->
<li class="{% active request direktorat_list_url %}"><a href="{{ direktorat_list_url }}">Direktorat</a></li>

You can do it with some conditions like `{% if request.resolver_match.url_name == "name" %}` or `{% if request.get_full_path == '/mypage/' %}`, then for example like this:

{% load nav_menus_active_tags %}
{% url 'popular_videos_page' as popular_videos_page %}
   <li class="{% if request.resolver_match.url_name == "popular_videos_page" %}{% active request popular_videos_page %}{% endif %}">
      <a class="nav-popular" href="{% url 'popular_videos_page' %}"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span> Popular</a>

May it usefully…

101 1 1 0