Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 2.0 - Not a valid view function or pattern name (Customizing Auth views)

I´m working on a course exercise and I'm stuck for a few hours and I'm not sure what is causing the app to break, next, you will find the files involved and perhaps you can find out the solution. Thanks for your help!

Project structure

This error is being thrown when I log in:

Internal Server Error: /account/login/

...


    django.urls.exceptions.NoReverseMatch: Reverse for 'dashboard' not found. 'dashboard' is not a valid view function or pattern name.
    [04/Apr/2018 17:12:15] "POST /account/login/ HTTP/1.1" 500 151978

At the end of the settings.py file

from django.urls import reverse_lazy

LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_REDIRECT_URL = reverse_lazy('logout')

The urls.py file

from django.contrib.auth import views as auth_views
from django.urls import path
from . import views

app_name = 'account'

urlpatterns = [
    # path('login/', views.user_login, name='login'),
    path('', views.dashboard, name='dashboard'),

    # login / logout urls
    path('login/', auth_views.LoginView.as_view(template_name='registration/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='registration/logged_out.html'), name='logout'),
    path('logout-then-login/', auth_views.logout_then_login, name='logout_then_login'),
]

The views.py file

from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render


@login_required
def dashboard(request):
    return render(request, 'account/dashboard.html', {'section': 'dashboard'})

The base.html template

{% load staticfiles %}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{% block title %}{% endblock %}</title>
    <link rel="stylesheet" href="{% static "css/base.css" %}">
</head>
<body>
    <div id="header">
        <span class="logo">Bookmarks</span>
        {% if request.user.is_authenticated %}
            <ul class="menu">
                <li> {% if section == "dashboard" %}class="selected"{% endif %}><a href="{% url "account:dashboard" %}">My dashboard</a></li>
                <li> {% if section == "images" %}class="selected"{% endif %}<a href="#">Images</a></li>
                <li> {% if section == "people" %}class="selected"{% endif %}<a href="#">People</a></li>
            </ul>
        {% endif %}

        <span class="user">
            {% if request.user.is_authenticated %}
                Hello {{ request.user.first_name }}, <a href="{% url "account:logout %}">Logout</a>
            {% else %}
                <a href="{% url "account:dashboard" %}"></a>
            {% endif %}
        </span>
    </div>

    <div id="content">
        {% block content %}
        {% endblock %}
    </div>
</body>
</html>

I appreciate your help. Thanks a lot!

like image 798
ralfillo Avatar asked Apr 04 '18 15:04

ralfillo


2 Answers

You've set a namespace for your urls:

app_name = 'account'

You need to use that namespace when reversing urls with reverse/reverse_lazy or {% url %}:

LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_REDIRECT_URL = reverse_lazy('account:logout')
like image 155
knbk Avatar answered Oct 24 '22 03:10

knbk


Perhaps when specifying {% url 'appname:views' %} you specified the wrong appname

For example, like:

wrong - {% url 'accuant:dashboard' %}
right - {% url 'account:dashboard' %}
like image 34
Maxocoder Avatar answered Oct 24 '22 04:10

Maxocoder