0

I have a navigation bar as below:

<ul class="nav nav-tabs">
      <li role="presentation" class="active"><a href="{% url 'list_view' %}">List View</a></li>
      <li role="presentation"><a href="{% url 'table_view' %}">Matrix View</a></li>
</ul>

Actually its a django template. What I need to achieve is whenever the user select a navigation option, user can clearly see which one he selected. Means the active must change to the selected option. But currently its defaulted to the first navigation option. How can I change that? Thanks in advance.

vellattukudy
  • 789
  • 2
  • 11
  • 25
  • 1
    Possible duplicate of [Django dynamically get view url and check if its the current page](http://stackoverflow.com/questions/22047251/django-dynamically-get-view-url-and-check-if-its-the-current-page) – Sayse Mar 14 '16 at 08:36

1 Answers1

0

You can write context processor for pass to template current url name:

from django.core.urlresolvers import resolve

def default(request):
    return {
        'current_urlname': resolve(request.path).url_name,
    }

and check active link like this:

<li role="presentation"{% if current_urlname == 'table_view' %} class="active"{% endif %}>
    <a href="{% url 'table_view' %}">Matrix View</a>
</li>
LinnTroll
  • 705
  • 4
  • 10