Re: Http handler may assign urlresolver-related data to request

113 views
Skip to first unread message

Aymeric Augustin

unread,
Sep 25, 2012, 7:59:19 AM9/25/12
to django-d...@googlegroups.com
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.


-- 
Aymeric.

2012/9/25 Benoit Bryon <benoit...@novapost.fr>
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#L222 or 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 django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.



--
Aymeric.

Florian Apolloner

unread,
Sep 25, 2012, 10:00:48 AM9/25/12
to django-d...@googlegroups.com
Hi Benoit,

as a matter of fact I want to add that to 1.5, and I started playing with a small testapp to see what's needed: https://github.com/apollo13/django-locale-switcher -- My conclusion is also that stuffing the resolver_match on the request would be the best option. We have another five days till feature freeze and my time is currently needed somewhere else, so I probably only have time the last 1 or 2 days. If you can open a pull request with a fix + tests + docs I'll happily review and apply it. If you don't, I still might be able to do it the last days but that depends on my time, so no promises there.

Looking forward to see some code from you,
Florian

Florian Apolloner

unread,
Sep 27, 2012, 9:13:40 AM9/27/12
to django-d...@googlegroups.com
I added a patch to https://github.com/django/django/pull/399 -- Let me know what you think, if I don't get any negative feedback I'll commit it before the feature freeze.

Cheers,
Florian
Reply all
Reply to author
Forward
0 new messages