django - Get name for matched URL pattern -


I am writing a Django reference processor, which needs access to the URL pattern, which was successfully resolved. is. Looking at the pattern,

  url (r '^ home / $', 'app.index', name = 'website.home')  

And the request path / home, I want to get the value for the name, which is the website in this case. Home.

I have received this code from djangosnippets.org:

  def _get_named_patterns (): Returns a list of "" "(pattern-name, pattern) topleps." "Resolver = urlresolvers.get_resolver (None) Pattern = Sorted (key, val [0] [0] [0]) for key, Resolver in Resolver. Resverse_dict.iteritems () If isinstance (Key, BasString) Return Pattern  

I can use it to achieve my purpose but my gut feeling that there should be a better way. Thanks for the help.

Regarding doing this request through objects and middleware Like:

  Category MyMiddleware (object): def process_view (auto, request, view_funk, view_orge, view_corrh): If view_kwargs.get ('name', none): request. Do not reset any  

name = view_kwargs ('name', none) and using default context launcher in settings.py: django.core.context_processors.request "< / Code>

Then you can get the name everywhere by using request.name after the middleware is executed.

Cheers,


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -