Namespace not solving NoReverseMatch: Reverse for 'login' not found. 'login' is not a valid view function or pattern name
I have this error and don't know exactly the source of it, if it's an error in the apps urls, or in the projects urls. The exact error is "NoReverseMatch: Reverse for 'login' not found. 'login' is not a valid view function or pattern name." The code is where the problem is:
<li class="nav-item">
{% if not user.is_authenticated %}
<a class="nav-link" href="{% url 'login' %}">Login</a>
{% else %}
<a class="nav-link" href="{% url 'logout' %}">Logout</a>`
{% endif %}
</li>
Here is my urls: from django.contrib import admin from django.urls import path, include from . import views from django.contrib.auth import views as auth_views from .forms import *
urlpatterns = [
path('', views.index, name="index"),
path('admin/', admin.site.urls),
path('register/', views.UserSignupView.as_view(), name="register"),
path('login/', auth_views.LoginView.as_view(template_name="login.html", authentication_form=UserLoginForm)),
path('logout/', views.logout_user, name="logout"),
]
And my projects urls:
from django.contrib import admin from django.urls import path, include
urlpatterns = [
path('', include('templates.urls')),
path('admin/', admin.site.urls),
]
Disclaimer, I have tried adding the namespace, but this flags another issue, so wanted to see if there is any other issues with the code.
I have tried to see if the problem is in the projects urls, but to no success. Have tried to see if it's a form problem, but the form has no problem.
You need to name the URL properly, like:
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('register/', views.UserSignupView.as_view(), name='register'),
path(
'login/',
auth_views.LoginView.as_view(
template_name='login.html', authentication_form=UserLoginForm
),
name='login',
),
path('logout/', views.logout_user, name='logout'),
]
You didn't write a name for "login" url. You must add name="login" to end of path.
path('login/', auth_views.LoginView.as_view(template_name="login.html", authentication_form=UserLoginForm), name="login"),