Login

Active link

Author:
ronnie
Posted:
November 30, 2011
Language:
Python
Version:
1.3
Tags:
template path active link current
Score:
2 (after 2 ratings)

I needed a way to find if a menu items should be active. After searching the internet i found a few options*, but none of them did fit my needs, so i wrote my own:

Usage: <a href="{% url 'view-name' %}" class="{% current request 'view-name' %}"></a>

  • http://gnuvince.wordpress.com/2008/03/19/the-new-and-improved-active-tag/
  • http://stackoverflow.com/questions/340888/navigation-in-django
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import re

from django import template
from django.core.urlresolvers import get_resolver

register = template.Library()

@register.simple_tag
def current(request, view):
    possible_results = get_resolver(None).reverse_dict.getlist(view)

    if len(possible_results) != 0 and re.match(possible_results[0][1], request.path[1:]):
        return 'current'
    return ''

More like this

  1. Active page class for selected menu items by kunitoki 2 years, 8 months ago
  2. load m2m fields objects by dirol 4 years, 11 months ago
  3. Pagination Alphabetically compatible with paginator_class by vascop 3 years, 1 month ago
  4. Active class for navigation link by cschand 5 years, 8 months ago
  5. Using reverse() to do redirects by ubernostrum 7 years, 9 months ago

Comments

Please login first before commenting.