Monday, March 8, 2010

Django current 'active' page highlighting for navigation

It is pretty common to want to highlight the current page in your navigation bar. Unfortunately it is surprisingly hard to do with django. After reading a few blogs I decided on the following, which involves creating a custom template tag and enabling the request context processor. First create a custom template tag (don't forget the in your templatetags directory):

#!/usr/bin/env python

from django import template
register = template.Library()

def active(request, pattern):
import re
if, request.path):
return 'current_page_item'
return ''

Add names to your views in so you can reference the regex in the template. The prototype is '(regular expression, Python callback function [, optional dictionary [, optional name]])':

(r'^$', 'home view', {}, 'home')

Then in your template:

{% load active %}
{% url home as home %}

<li class="{% active request home %}">Home</li>

In define the 'request' context processor (you also have to define the other defaults so you don't lose them):

TEMPLATE_CONTEXT_PROCESSORS = ('django.core.context_processors.auth',

Now you just need a line in your CSS that does somehthing different for the 'current_page_item' class.


Dhiraj Thakur said...

You can also do this when you are rendering the template...

i added {% block activepage %}{% endblock %} in my base template and in a page which extended from the basic template i added {% block activepage %} class="active" {% endblock %} where active class does the highlight in CSS.

Hope this also helps somebody... :)

G said...

@Dhiraj: not sure I understand your comment.

This post is about applying different style to an item in a navigation bar. With your example how do you know which item should have class="active"?

iannakin said...

I actually like Dhiraj Thakur's method, it's not best practices, but it's really quick to put up.

Let's say you have such a template hierarchy :

templates/index.html (extends base.html)
templates/about.html (extends base.html)

You could have something like :

<li class="{% block link_home_class %}{% endblock %}"><a href="/">Home</a>
<li class="{% block link_about_class %}{% endblock %}"><a href="/about">About</a>

Then, in your derivative templates :

{% extends "base.html" %}
{% block link_home_class %}active{% endblock %}

{% extends "base.html" %}
{% block link_about_class %}active{% endblock %}

Now you just have to style your active li with something like :

ul {
/* some css code */

or, to style the anchor directly
ul a {
/* some css code */