#!/usr/bin/env python
#project/site/templatetags/active.py
from django import template
register = template.Library()
@register.simple_tag
def active(request, pattern):
import re
if re.search(pattern, request.path):
return 'current_page_item'
return ''
Add names to your views in urls.py 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 settings.py 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',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.request')
Now you just need a line in your CSS that does somehthing different for the 'current_page_item' class.