Normally Django appends /
to the URLs, so it might be the case of changing to:
{% if request.path == '/phylosophy/' %}class="active"{% endif %}>
Extra:
The thing with this approach is that, if you have a deeper URL like /phylosophy/list/
, you may still want to keep the active
class, so what I usually do is creating a templatetag called startswith
:
@register.filter('startswith')
def startswith(text, starts):
if isinstance(text, basestring):
return text.startswith(starts)
return False
And then use it like:
<li{% if request.path|startswith:'/phylosophy/' %} class="active"{% endif %}>
PS:
In case request.path
is empty when you print it, you might need to add it to your context processors (django.template.context_processors.request
), example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': (
PROJECT_DIR.child('templates'),
),
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.request',
],
'debug': DEBUG
}
},
]