Opened 6 years ago

Closed 6 years ago

#29611 closed Bug (needsinfo)

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

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 (last modified by Tim Graham)

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"

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 (1)

comment:1 by Tim Graham, 6 years ago

Description: modified (diff)
Resolution: needsinfo
Status: newclosed

The {% url %} template tag uses the reverse() function. You'll need to investigate the issue and explain exactly where the bug is because I'm doubtful that this can't be explained by an expected behavior. By the way, the URL that's returned is based on the order of urlpattterns; INSTALLED_APPS doesn't matter.

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