Search code examples
pythondjangonamespacesdjango-urlsdjango-authentication

What are the default namespaces for django authentication URLs?


I changed my app's urls.py to have a namespace app_name = 'somename'. And then did these,

  1. Changed reverse('url-name') to reverse('somename:url-name').
  2. Changed {% url 'url-name' %} to {% url 'somename:url-name' %}.
  3. Changed a {% if request.resolver_match.url_name == "url-name" %} to {% if request.resolver_match.url_name == "somename:url-name" %}.

For accounts app, I only have two custom views for login and signup. So a reverse like reverse('accounts:login') is working but reverse('accounts:password_reset') (part of default django auth) is not working.

Here is what I tried,

  1. Replaced reverse('accounts:password_reset') with reverse('auth:password_reset').
  2. Replaced reverse('accounts:password_reset') with reverse('password_reset').

Solution 1 did not work. Solution 2 is working. I also tried changing path('accounts/', include('django.contrib.auth.urls')) in my project's urls.py to path('accounts/', include('django.contrib.auth.urls', namespace='auth')) and path('accounts/', include('django.contrib.auth.urls, namespace='accounts')). Neither worked.

project/urls.py

urlpatterns = [
    path('', include('core.urls')),
    path('admin/', admin.site.urls),
    path('accounts/', include('accounts.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('books/', include('library.urls'))
]

accounts/urls.py

app_name = 'accounts'
urlpatterns = [
    path('signup/', views.CustomSignup.as_view(), name='signup'),
    path('login/', views.CustomLogin.as_view(), name='login'),
]  

Why namespacing password_reset with accounts and auth fail? Is there a documentation page that has default namespaces that can be used?


Solution

  • As far as I can tell by reading the source code they are not namespaced at all, you can normally only refer to them by name as you are now.

    If you want to have them namespaced, you could include them into your accounts' urlpatterns, rather than your project's and add the namespace manually:

    app_name = 'accounts'
    urlpatterns = [
        path('signup/', views.CustomSignup.as_view(), name='signup'),
        path('login/', views.CustomLogin.as_view(), name='login'),
        path('', include('django.contrib.auth.urls', namespace='accounts')),
    ]