NoReverseMatch: Reverse for 'login' not found. 'login' is not a valid view function or pattern name

23 Views Asked by At

I am sorry to post this problem once again.

I am getting the error even though the configuration files looks correct with the 'login' entry.

In settings.py added

LOGIN_URL = '/login/'

In the APP/templates/base.html:

        {% if user.is_authenticated %}
            <a href="{% url 'logout' %}">Logout</a>
        {% else %}
            <a href="{% url 'login' %}">Login</a>
        {% endif %}

The APP/views.py looks as follows:

from django.http import HttpResponse
from django.template import loader
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required

@login_required(login_url='login') # Redirect to the login page if not authenticated
def protected_intro(request):
    return render(request, 'intro.html')

The APP/urls.py looks like:

from django.urls import path
from django.contrib.auth.views import LoginView
from . import views

app_name = 'APP'

urlpatterns = [
    ...
    path('login/', LoginView.as_view(), name='login'),
]

All the entries regarding the 'login' seems correct, but ending up with the error "Reverse for 'login' not found. 'login' is not a valid view function or pattern name."

Please help.

Thanks!

I tried looking for the solution, but could not solve it.

1

There are 1 best solutions below

0
IVNSTN On

app_name = 'APP' makes your url pattern names prefixed with this namespace. Now you need to refer to those url patterns as {% url 'APP:login' %}

or remove app_name = 'APP'