- Author:
 - kunitoki
 - Posted:
 - September 26, 2012
 - Language:
 - Python
 - Version:
 - 1.4
 - Score:
 - 1 (after 1 ratings)
 
Simple tag to check which page we are on, based on resolve: useful to add an 'active' css class in menu items that needs to be aware when they are selected.
Typical usage is like:
<ul>
    <li class="{% active request "myapp:myview1" %}">My View 1</li>
    <li class="{% active request "myapp:myview2" %}">My View 2</li>
</ul>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44  | from django import template
from django.core.urlresolvers import resolve
register = template.Library()
class ActiveUrlNode(template.Node):
    def __init__(self, request, names, return_value='active'):
        self.request = template.Variable(request)
        self.names = [template.Variable(n) for n in names]
        self.return_value = template.Variable(return_value)
    def render(self, context):
        request = self.request.resolve(context)
        any_of = False
        try:
            url = resolve(request.path_info)
            url_name = "%s:%s" % (url.namespace, url.url_name)
            for n in self.names:
                name = n.resolve(context)
                if url_name.startswith(name):
                    any_of = True
                    break
        except:
            # TODO - think a better way to log these
            print "Cannot resolve %s" % request.path_info
        return self.return_value if any_of else ''
@register.tag
def active(parser, token):
    """
        Simple tag to check which page we are on, based on resolve;
        Useful to add an 'active' css class in menu items that needs to be
        aware when they are selected.
        Usage:
            {% active request "base:index" %}
            {% active request "base:index" "base:my_view" %}
    """
    try:
        args = token.split_contents()
        return ActiveUrlNode(args[1], args[2:])
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires at least 2 arguments" % token.contents.split()[0]
 | 
More like this
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 4 weeks ago
 - get_object_or_none by azwdevops 5 months, 2 weeks ago
 - Mask sensitive data from logger by agusmakmun 7 months, 2 weeks ago
 - Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
 - JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
 
Comments
You need to add
register = template.Library()
at the top to make this snippet work; just FYI for newer folks to Django.
#
Snippet updated thanks ;)
#
Nice! two important details that should be added for newbies like me : * you should add "django.core.context_processors.request" to your TEMPLATE_CONTEXT_PROCESSORS in settings.py (TEMPLATE_CONTEXT_PROCESSORS might have to be added -> https://docs.djangoproject.com/en/1.5/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS * when not using namespaced route names, you still need the colon : {% active request ":home" %}
#
Please login first before commenting.