Opened 11 years ago

Last modified 7 years ago

#4311 closed

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

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: UI/UX:

Description (last modified by Jeremy Dunck)

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 (
    settings.LOGIN_REDIRECT_URL,
    settings.LOGIN_URL,
    settings.LOGOUT_URL)
  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. :)
http://www.djangoproject.com/documentation/url_dispatch/#naming-url-patterns

Change History (1)

comment:1 Changed 11 years ago by Jeremy Dunck

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top