Hi BenoƮt, Thanks for the suggestion. It's tracked in https://code.djangoproject.com/ticket/15695. This ticket hasn't seen much activity, but I've heard this feature request several times.
I don't see much harm in adding the ResolverMatch as an attribute of the request. ResolverMatch is already a public API [1]. Overall I'm +0 on the idea. [1] https://docs.djangoproject.com/en/1.4/topics/http/urls/#django.core.urlresolvers.ResolverMatch -- Aymeric. 2012/9/25 Benoit Bryon <[email protected]> > Hi, > > Here is a feature request : as a developer, I'd like to know the current > URL name, app name or namespace (or any information gathered during URL > resolution) within views, templates... i.e. attached to a request object. > > Some use cases: > > * display some content only if inside an application > * set a menu item as active if url_name of current request matches > url_name of menu link > * enable a context_processor if only within some application (performance > tuning). > > As far as I know, this feature isn't available currently. > > We do have request.path_info. Is it enough? It works, sometimes. But > that's not a rule. As an example, when URL are localized, request.path_info > changes. Request.path_info is not a reference a developer should rely on. > Whereas named URL patterns seems suitable (and even, made for that purpose). > > Looking at the code, I'm supposing such a feature could be implemented > quite easily: > > * During URL resolution, resolvers return a ResolveMatch object (see > https://github.com/django/django/blob/e72e22e518a730cd28cd68c9374fa79a45e27a9c/django/core/urlresolvers.py#L222or > https://github.com/django/django/blob/e72e22e518a730cd28cd68c9374fa79a45e27a9c/django/core/urlresolvers.py#L331) > > > * ResolveMatch instances have attributes such as app_name, url_name... > That's what I'm looking for. See > https://github.com/django/django/blob/e72e22e518a730cd28cd68c9374fa79a45e27a9c/django/core/urlresolvers.py#L39 > > * HTTP handlers are the ones which manage both the URL resolution and the > request object. They could assign url_name or app_name to request. See > https://github.com/django/django/blob/e72e22e518a730cd28cd68c9374fa79a45e27a9c/django/core/handlers/base.py#L104 > > I mean, in > https://github.com/django/django/blob/e72e22e518a730cd28cd68c9374fa79a45e27a9c/django/core/handlers/base.py#L104, > we have: > > callback, callback_args, callback_kwargs = >> resolver.resolve(request.path_info) >> > > But if we had something like: > > request.resolver_match = resolver.resolve(request.path_info) >> callback, callback_args, callback_kwargs = request.resolver_match >> > > Then, in templates, we could write something like: > > <a href="{% url foo %}" {% if request.resolver_match.url_name == 'foo' >> %}class="active"{% endif %}>Foo</a> >> > > May I create a ticket for this feature request? > > Benoit > > -- > You received this message because you are subscribed to the Google Groups > "Django developers" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-developers/-/FHIq4ryF5uAJ. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/django-developers?hl=en. > -- Aymeric. -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
