Opened 6 years ago

Last modified 3 years ago

#16406 new Cleanup/optimization

Allow separate access to matches from urlpatterns and extra_context args

Reported by: Florian Apolloner Owned by: nobody
Component: Core (URLs) Version: master
Severity: Normal Keywords: resolvers, reverse
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Currently Django's ResolverMatch (eg the one returned by urlresolvers.resolve) gives access to args and kwargs which are ready to be passed to the func being resolved. This does eliminate the possibility of an interesting use-case:

Let's assume I have a page written in english and german. I configured my urls using the new urlpatterns like this:

urlpatterns = i18n_patterns('',
    url(_(r'^about/$'), 'about.view', {'some_extra': 'data'}, name='about'),
    url(_(r'^news/$'), 'home.view', {'some_extra': 'data'}, name='home'),
)

I now would like to give my visitors an easy way to access the content in german. So my /about should have a link to /über. To do that, one could write a templatetag which does the following:

match = urlresolvers.resolve('current-url')
links = []
for lang in settings.LANGUAGES:
  translation.activate(lang):
  links.append(reverse(match.url_name, args=match.args, kwargs=match.kwargs))
# Further logic to display the links in the template

This doesn't work since the ResolverMatch combined the kwargs captured from the url-regex with the dictionary supplied for the extra data.

I think it would be a great if the ResolverMatch would give access to kwargs and extra_context separately. There might be more usecases, but that's the only one I can come up with for now. From a quick view at the code it should be possible to do that backwards-compat (eg ResolverMatch.__getitem__ would still return the current result but internally it would use two dictionaries).

Attachments (3)

t16406.diff (6.4 KB) - added by Florian Apolloner 6 years ago.
simple patch demonstrating the feature
t16406_new.diff (6.5 KB) - added by Florian Apolloner 6 years ago.
valueslisterrormessage3.diff (8.1 KB) - added by antoviaque 6 years ago.

Download all attachments as: .zip

Change History (11)

Changed 6 years ago by Florian Apolloner

Attachment: t16406.diff added

simple patch demonstrating the feature

comment:1 Changed 6 years ago by Aymeric Augustin

Has patch: set
Triage Stage: UnreviewedDesign decision needed
Type: UncategorizedCleanup/optimization

comment:2 Changed 6 years ago by Jacob

Triage Stage: Design decision neededAccepted

Good idea!

comment:3 Changed 6 years ago by antoviaque

Owner: changed from nobody to antoviaque

Changed 6 years ago by Florian Apolloner

Attachment: t16406_new.diff added

comment:4 Changed 6 years ago by Florian Apolloner

New and cleaner patch, but it does change behavior (see changes of the doc file)

Changed 6 years ago by antoviaque

comment:5 Changed 6 years ago by antoviaque

Owner: changed from antoviaque to nobody

Looks like you beat me to the update apollo : )

I've added a test specific to the behavior change though, you may want to integrate it in your patch.

comment:6 Changed 6 years ago by anonymous

If your patch includes my changes that should be fine…

comment:7 Changed 5 years ago by Aymeric Augustin

Component: Core (Other)Core (URLs)

comment:8 Changed 3 years ago by Tim Graham

Patch needs improvement: set

Patch no longer applies cleanly.

Note: See TracTickets for help on using tickets.
Back to Top