Opened 12 years ago

Last modified 8 years ago

#4311 closed

urlresolvers.resolve and friends should return the name they resolved to. — at Initial Version

Reported by: Jeremy Dunck <jdunck@…> Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Now that urlresolvers.reverse and named URLPatterns are in place, I think it'd be good for resolve to return the name in addition to the callable and args/kwargs.

This would allow app-level branching depending on URL to be simpler.

As an example from a django-users list thread:

  is_login = request.path in (
  if ((not is_login) and ...

If urlresolvers.resolve() returned, (callable, args, kwargs, name) rather than (callable, args, kwargs), (where name is the name assigned in the URLPattern definition), you could have simpler code that inspected URL names and decided what to do with less muss.

if urlresolvers.resolve(url)[3].startswith('contrib.auth.urls.') 

The 3 is icky and auth doesn't actually include an urls file, but you get the point. ;-)

Further, it'd be good if names were nested based on includes in a way that mirrors URLResolver includes so that the name of anything included by an app was prefixed by that app name.

For example:
urlpatterns = (

('admin/', include('contrib.admin.urls')


Assuming the admin urls definition was changed to include things like this:

('([/]+)/([/]+)/$', 'django.contrib.admin.views.main.change_list', name='change_list'),

Then a request path resolving to the admin change list would include a return name of 'contrib.admin.urls.change_list'.

I imagine the advice that urlconf names be global-ish is just due to current implementation limitation. :)

Change History (0)

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