Django 2.0 - 不是有效的视图函数或模式名称(自定义 Auth 视图) [英] Django 2.0 - Not a valid view function or pattern name (Customizing Auth views)

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

问题描述

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

项目结构

登录时抛出此错误:

内部服务器错误:/account/login/...django.urls.exceptions.NoReverseMatch:未找到仪表板"的反转.仪表板"不是有效的视图函数或模式名称.[04/Apr/2018 17:12:15] "POST/account/login/HTTP/1.1" 500 151978

settings.py 文件的末尾

from django.urls import reverse_lazyLOGIN_REDIRECT_URL = reverse_lazy('仪表板')LOGIN_URL = reverse_lazy('登录')LOGOUT_REDIRECT_URL = reverse_lazy('退出')

urls.py 文件

from django.contrib.auth 将视图导入为 auth_views从 django.urls 导入路径从 .导入视图app_name = '账户'网址模式 = [# path('login/', views.user_login, name='login'),path('', views.dashboard, name='dashboard'),# 登录/注销网址路径('登录/', auth_views.LoginView.as_view(template_name='registration/login.html'), name='login'),路径('注销/', auth_views.LogoutView.as_view(template_name='registration/logged_out.html'), name='注销'),路径('注销然后登录/', auth_views.logout_then_login, name='logout_then_login'),]

views.py 文件

from django.contrib.auth import authentication, login从 django.contrib.auth.decorators 导入 login_required从 django.http 导入 HttpResponse从 django.shortcuts 导入渲染@需要登录定义仪表板(请求):返回渲染(请求,'account/dashboard.html',{'section':'dashboard'})

base.html 模板

{% 加载静态文件 %}<!doctype html><html lang="zh-cn"><头><meta charset="UTF-8"><元名称=视口"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 %}<link rel="stylesheet" href="{% static "css/base.css" %}"><身体><div id="标题"><span class="logo">书签</span>{% if request.user.is_authenticated %}<ul class="菜单"><li>{% if section == "dashboard" %}class="selected"{% endif %}><a href="{% url "account: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>{% 万一 %}<span class="user">{% if request.user.is_authenticated %}你好 {{ request.user.first_name }}, <a href="{% url "account:logout %}">Logout</a>{% 别的 %}<a href="{% url "account:dashboard" %}"></a>{% 万一 %}</span>

<div id="内容">{% 块内容 %}{% 结束块 %}

感谢您的帮助.非常感谢!

解决方案

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

app_name = 'account'

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

LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')LOGIN_URL = reverse_lazy('账户:登录')LOGOUT_REDIRECT_URL = reverse_lazy('账户:注销')

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!

解决方案

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

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

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆