pete
pete

Reputation: 2799

Django Templates: How to filter based on the URL?

I have a common header used throughout the site. On some pages, based on the URI we're at, I'd like to include/exclude some html content. What would be the proper way to do it? I see a 'url' tag but that obviously isn't it (doesn't seem to work for me at all in fact, on Django 1.5.5). I was hoping for a simple filter, something like:

{% if url == '/dashboard/' %}
   <!-- conditional html content -->
{% endif %}

I know that I could pass some custom data from the View action via its context, then check against it in the template, but that feels a bit.. excessive/iffy (?)

Upvotes: 0

Views: 150

Answers (1)

Sam Dolan
Sam Dolan

Reputation: 32532

You can use request.path in the template to get the current url like so:

{% if request.path == '/dashboard/' %}
   <!-- conditional html content -->
{% endif %}

For this to work you must have django.core.context_processors.request listed in your TEMPLATE_CONTEXT_PROCESSORS in your settings.py file.

Upvotes: 1

Related Questions