Python Django 2.0-不是有效的视图函数或模式名称(自定义身份验证视图)

Python Django 2.0-不是有效的视图函数或模式名称(自定义身份验证视图),python,django,authentication,django-templates,Python,Django,Authentication,Django Templates,我正在做一个课程练习,我被困了几个小时,我不确定是什么原因导致应用程序崩溃,接下来,你会找到涉及的文件,也许你可以找到解决方案。谢谢你的帮助 在我登录时引发此错误: Internal Server Error: /account/login/ ... django.urls.exceptions.NoReverseMatch: Reverse for 'dashboard' not found. 'dashboard' is not a valid view function o

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

在我登录时引发此错误:

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文件的末尾

from django.urls import reverse_lazy

LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_REDIRECT_URL = reverse_lazy('logout')
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'),
]
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'})
{% 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>
url.py文件

from django.urls import reverse_lazy

LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_REDIRECT_URL = reverse_lazy('logout')
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'),
]
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'})
{% 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>
views.py文件

from django.urls import reverse_lazy

LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_REDIRECT_URL = reverse_lazy('logout')
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'),
]
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'})
{% 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>
base.html模板

from django.urls import reverse_lazy

LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_REDIRECT_URL = reverse_lazy('logout')
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'),
]
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'})
{% 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>
{%load staticfiles%}
{%block title%}{%endblock%}
书签
{%if request.user.u经过身份验证%}
  • {%if section==“dashboard”%}class=“selected”{%endif%}>
  • {%if section==“images”%}class=“selected”{%endif%}
  • {%if section==“people”%}class=“selected”{%endif%}
{%endif%} {%if request.user.u经过身份验证%} 你好{{request.user.first_name}}, {%else%} {%endif%} {%block content%} {%endblock%}

我感谢你的帮助。非常感谢

您已经为URL设置了名称空间:

app_name = 'account'
使用
reverse
/
reverse\u lazy
{%url%}
反转url时,需要使用该命名空间:

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

可能在指定{%url'appname:views%}时指定了错误的appname

例如:

wrong - {% url 'accuant:dashboard' %}
right - {% url 'account:dashboard' %}