Skip to content
Advertisement

NoReverseMatch at /login/ Reverse for ” not found. ” is not a valid view function or pattern nam

I don’t know why this problem is occuring. The login page is being rendered fine. But when I am clicking the Log In button after filling out the forms, I am getting this error. Can anyone help me solve this?

My settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = []

import sys
sys.modules['fontawesome_free'] = __import__('fontawesome-free')

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'class2.apps.Class2Config',
    'crispy_forms',
    'django_bootstrap_icons',
    'fontawesome_free',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'abrar_class.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'abrar_class.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_URL = 'static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

LOGIN_REDIRECT_URL = ""

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

My urls.py:

from django import VERSION
from django.urls import path
from django.contrib.auth import views as auth_views
from .views import CourseDetailView, CourseListView, YourCourses, ProfileDetailView
from . import views

urlpatterns = [
    path("", CourseListView.as_view(), name="index"),
    path("login/", auth_views.LoginView.as_view(template_name='class/login.html'), name='login'),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("<slug>", CourseDetailView.as_view(), name="course"),
    path('get_enrolled/<slug>/', views.get_enrolled, name='get_enrolled'),
    path('your_courses/', YourCourses.as_view(), name='your_courses'),
    path('profile/<int:pk>/', ProfileDetailView.as_view(), name='profile'),
]

My login.html file:

{% extends "class/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <br>
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group" style="color: #f5a425">
                <legend class="border-bottom mb-4" style="color:#f5a425;">Log In</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-warning" type="submit">Login</button>
            </div>
        </form>
        <div class="border-top pt-3">
            <small class="text-muted">
                Need An Account? <a class="ml-2" href="{% url 'register' %}">Sign Up</a>
            </small>
        </div>
    </div>
{% endblock content %}

Advertisement

Answer

You’ve specified LOGIN_REDIRECT_URL = "". The documentation for this setting says:

The URL or named URL pattern where requests are redirected after login when the LoginView doesn’t get a next GET parameter.

This means what is happening is that Django is assuming you to have specified a url name instead of a url. You can instead set the setting as follows if you want to redirect to the home page:

LOGIN_REDIRECT_URL = "/"

Or you can actually use the url name:

LOGIN_REDIRECT_URL = "index"
Advertisement