Django 无法显示提供的注销模板

Posted

技术标签:

【中文标题】Django 无法显示提供的注销模板【英文标题】:Django unable to display the logout template provided 【发布时间】:2021-03-29 01:22:06 【问题描述】:

我有以下问题;我尝试使用默认的 contrib.auth 视图设置用户登录和注销,但显示的注销页面是 django admin 的默认页面,而登录页面没有问题并显示我提供的支持模板

django-tutorial/urls.py

from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path, include
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('landingpage.urls')),
    path('articles/', include('articles.urls')),
    path('accounts/', include('accounts.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

accounts/urls.py

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

app_name = 'accounts'

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('password_change/', auth_views.PasswordChangeView.as_view(), name='password_change'),
    path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),
    path('password_reset/', auth_views.PasswordResetView, name='password_reset'),
    path('password_reset/done/', auth_views.PasswordResetDoneView, name='password_reset_done'),
    path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

login.html

% extends "landingpage/base.html" %

% block page_content %

% if form.errors %
<p>Your username and password didn't match. Please try again.</p>
% endif %

% if next %
    % if user.is_authenticated %
    <p>Your account doesn't have access to this page. To proceed,
    please login with an account that has access.</p>
    % else %
    <p>Please login to see this page.</p>
    % endif %
% endif %

<form method="post" action="% url 'accounts:login' %">
% csrf_token %
<table>
<tr>
    <td> form.username.label_tag </td>
    <td> form.username </td>
</tr>
<tr>
    <td> form.password.label_tag </td>
    <td> form.password </td>
</tr>
</table>

<input type="submit" value="login">
<input type="hidden" name="next" value=" next ">
</form>

# Assumes you setup the password_reset view in your URLconf #
<p><a href="% url 'accounts:password_reset' %">Lost password?</a></p>

% endblock %

logged_out.html

% extends "landingpage/base.html" %

% block page_content %

<div class="container-fluid p-5">
    <h2>Logout</h2>
    <br>
    <p>Grazie per aver speso del tempo di qualità in questo sito oggi!</p>
    <p>Speriamo di rivederti presto</p>
    <br>
    <a href="% url 'accounts:login' %">Vuoi rifare il Login?</a>
</div>
% endblock %)

显示的注销页面是django admin的默认页面,而登录页面没有问题并显示我提供的支持模板

【问题讨论】:

能否分享来自settings.py文件的INSTALLED_APPS @Panos Angelopoulos 嘿,我已经解决了这个问题,但还是谢谢 【参考方案1】:

该类默认查看registration/logged_out.html 文件,但是,您也可以提供自定义位置:

path("logout/", 
     auth_views.LogoutView.as_view(template_name="accounts/logout.html"),
     name="logout"),

【讨论】:

以上是关于Django 无法显示提供的注销模板的主要内容,如果未能解决你的问题,请参考以下文章

Django 视图模板无法显示模型数据

我无法让这个外键表值显示在 django 模板中

Python Django 无法在一个模板中显示两个表单

无法将参数从模板传递到 Django 视图

django 中的表单占位符无法正确显示

django使用模板视图和ajax