Reputation: 1884
I have been doing lots of Googling on this topic and have gone through plenty of Django docs, but I can't seem to find a decent answer to what seems like ought to be a very simple question with a simple solution. So hopefully a Django vet out there can help.
Lets say I have the following urlconf:
urlpatterns = patterns('',
....
url(r'^directory/users/$', UserView.as_view(), name='users'),
url(r'^directory/users/(?P<user_id>[0-9]+)/$',UserView.as_view(), name='users'),
....
)
What I want to be able to do is test from within a template which pattern was followed, something like this:
{% if name_of_last_followed_url_pattern == 'users' %}
....
{% endif %}
Now one would think that Django would stash this away somewhere and be able to spit it back out to me. But I cannot find anything that corresponds to "name_of_last_followed_url_pattern" anywhere in the docs or in my searches. Any ideas how I might access this (or if not provided, why not)?
Upvotes: 0
Views: 51
Reputation: 2275
You need the context processors that adds the request to the template django.core.context_processors.request
added to the TEMPLATE_CONTEXT_PROCESSORS. Then in the template you can do
{% if request.resolver_match.url_name == 'users' %}
....
{% endif %}
resolver_match
has other attributes like namespaces, app_name. You can see here:
https://docs.djangoproject.com/en/1.6/ref/urlresolvers/#django.core.urlresolvers.ResolverMatch
Upvotes: 3