Opened 5 years ago

Last modified 2 years ago

#16406 new Cleanup/optimization

Allow separate access to matches from urlpatterns and extra_context args

Reported by: apollo13 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


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:
  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 apollo13 5 years ago.
simple patch demonstrating the feature
t16406_new.diff (6.5 KB) - added by apollo13 5 years ago.
valueslisterrormessage3.diff (8.1 KB) - added by antoviaque 5 years ago.

Download all attachments as: .zip

Change History (11)

Changed 5 years ago by apollo13

simple patch demonstrating the feature

comment:1 Changed 5 years ago by aaugustin

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed
  • Type changed from Uncategorized to Cleanup/optimization

comment:2 Changed 5 years ago by jacob

  • Triage Stage changed from Design decision needed to Accepted

Good idea!

comment:3 Changed 5 years ago by antoviaque

  • Owner changed from nobody to antoviaque

Changed 5 years ago by apollo13

comment:4 Changed 5 years ago by apollo13

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

Changed 5 years ago by antoviaque

comment:5 Changed 5 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 5 years ago by anonymous

If your patch includes my changes that should be fine…

comment:7 Changed 3 years ago by aaugustin

  • Component changed from Core (Other) to Core (URLs)

comment:8 Changed 2 years ago by timo

  • Patch needs improvement set

Patch no longer applies cleanly.

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