Django 2.0-无效的视图功能或模式名称(自定义Auth视图) [英] Django 2.0 - Not a valid view function or pattern name (Customizing Auth views)

查看:147
本文介绍了Django 2.0-无效的视图功能或模式名称(自定义Auth视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个课程练习,被困了几个小时,我不确定是什么原因导致应用程序崩溃,接下来,您将找到涉及的文件,也许您可​​以找到解决方法.感谢您的帮助!

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!

项目结构

我登录时抛出此错误:

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

settings.py 文件

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')

urls.py 文件

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'),
]

views.py 文件

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'})

base.html 模板

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!

推荐答案

您已经为网址设置了命名空间:

You've set a namespace for your urls:

app_name = 'account'

在使用reverse/reverse_lazy{% url %}反转URL时,需要使用该名称空间:

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')

这篇关于Django 2.0-无效的视图功能或模式名称(自定义Auth视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆