I am writing a Django context processor which needs to access the name of the URL pattern that the request got successfully resolved against. Given the pattern,
url(r'^home/$', 'app.index', name='website.home')
and the request path /home, I want to get the value for name, which in this case is website.home.
I got this code from djangosnippets.org:
def _get_named_patterns():
""" Returns a list of (pattern-name, pattern) tuples.
"""
resolver = urlresolvers.get_resolver(None)
patterns = sorted(
(key, val[0][0][0]) for key, val in resolver.reverse_dict.iteritems() if isinstance(key, basestring))
return patterns
I can use this to achieve my objective but my gut feeling says that there has to be a better method. Thanks for the help.