django内置身份验证视图未呈现模板

时间:2018-06-27 06:30:13

标签: python django

enter image description here我在 templates 文件夹下创建了 registration 文件夹,并将这些文件(login.html和logout.html)添加到该文件夹​​中。运行服务器我没有错误,但是我的登录模板未渲染。只有 base 模板是render。请指导我吗? 这是我的全部代码。

Login.html

{% extends 'base.html' %}
{% block title %}Login{% endblock %}
{% block content %}
    <h1>Log-in</h1>
    {% if form.errors %}
        <p>Your username and password didn't match. Please try again.</p>
    {% else %}
        <p>Please, use the following form to log-in. If you don't have an account <a href="{% url "register" %}">register here</a></p>
    {% endif %}

    <div class="login-form">
        <form action="{% url "login" %}" method="post">
            {{ form.as_p }}
            {% csrf_token %}
            <input type="hidden" name="next" value="{{ next }}" />
            <p><input type="submit" value="Log-in"></p>
        </form>
        <p><a href="#">Forgotten your password?</a></p>
    </div>
{% endblock %} 

urls.py

from django.urls import path
from django.contrib.auth.views import LogoutView, LoginView
from . import views

urlpatterns = [
    path('', views.dashboard, name='dashboard'),

    #login and logout
    path('login', LoginView.as_view(), name='login'),
    path('logout', LogoutView.as_view(), name='logout'),

]

views.py

from django.shortcuts import render
from django.contrib.auth.decorators import login_required


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

setting.py

from django.urls import reverse_lazy

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

base.html

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <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 "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 'logout' %}">Logout</a>
        {% else %}
            <a href="{% url 'login' %}">Login</a>
        {% endif %}
        </span>
    </div>

</body>
</html>

0 个答案:

没有答案