/accounts/login is not mapped when only root paths to its hosted app is not specified
I am using Django 5.2.3 and I am trying to use the built-in login system, for that I have defined the path inside my app users/urls.py
:
from django.urls import path, include
from . import views
urlpatterns = [
path("accounts/", include("django.contrib.auth.urls")),
]
If I go to the URL http://127.0.0.1:8000/accounts/login/
it will throw the following error:
The current path, accounts/login/, didn’t match any of these.
I fixed the issue by adding the following path to the URL dispatcher inside urls.py
at the project level (my project/urls.py
):
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('users.urls')) # This fixed the issue
]
And now the login form displays correctly at http://127.0.0.1:8000/accounts/login/
However, when I tried with a different path, like the following, it doesn't work:
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls')) # This doesn't work, same error thrown
]
Why it does work with path('', include('users.urls'))
and not path('users/', include('users.urls'))
?
Why it does work with
path('', include('users.urls'))
and notpath('users/', include('users.urls'))
?
Because the 'users/'
in path('users/', include('users.urls'))
says with what the path starts in order to look to the user.urls
.
Each path thus check if the path starts with a certain string, then "eats" that string, and lets the urls
it contains further handle it.
with:
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls'))
]
you can visit the login view, but at:
http://127.0.0.1:8000/users/accounts/login/
Since the root urls.py
checks if the path starts with users/
and remove users/
, then user.urls
checks if it starts with accounts/
and removes it, and finally the urls
module of django.contrib.auth
will check if the path starts (and ends) with login/
.