Opened 6 years ago

Last modified 6 years ago

#29611 closed Bug

Reverse and built-in template tag 'url' does not work exactly the same way. — at Initial Version

Reported by: Dr. Shubham Dipt Owned by: nobody
Component: Core (URLs) Version: 2.0
Severity: Normal Keywords: reverse, url, template tag
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Reverse: looks for the provided url name in each app in order of the apps mentioned in the INSTALLED_APPS in settings.py, however the url tag looks for the url name in the order of the urls provided in project/urls.py. If two urls with the same url name is provided but in different order in urls.py and different order of the respective app in INSTALLED_APPS, then the results (the final URL) will be different.

To explain the problem, lets have the example for url name: "login"
Order of INSTALLED_APPS = [

'django.contrib.admin',
'django.contrib.auth', # There lies a url named login with pattern /accounts/login
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'custom_app',

]

Now in the project/urls.py, I provide a different pattern for 'login',

urlpatterns = [

# Auth urls
re_path(r'login/$', auth_views.login, name='login'), # I removed the default accounts prefix from the login url

]

Now, in this case if I do:
reverse("login") -> the result will be /accounts/login
but if I do: url 'login' in a template -> the result will be /login/

Change History (0)

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