我目前正在学习 Django 身份验证,并且遇到了自定义 HTML 模板的问题。 Django 没有显示我的自定义模板,而是将我重定向到管理仪表板。
例如,当我将
LOGOUT_REDIRECT_URL
修改为 logout
并将 name
中的 TemplateView
修改为 logout
时,我希望看到我的自定义注销页面位于 templates/registration/logout.html
。但是,Django 继续将我重定向到默认的管理仪表板注销页面。但是当我将两者都设置为 customlogout
时,它会正确引导我。
同样,我也遇到了“忘记密码?”的问题。按钮。单击它时,我预计会被定向到
templates/registration/password_reset_form.html
,但相反,我被重定向到 Django 管理仪表板。
下面是我的代码设置:
设置.py:
LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = "customlogout"
应用程序urls.py:
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('logout/', TemplateView.as_view(template_name='logout.html'), name='customlogout'),
]
项目urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('account.urls')),
path("accounts/", include("django.contrib.auth.urls")),
]
密码重置模板
templates/registration/password_reset_form.html
:
<h1>Password Reset</h1>
<p>Enter your email ID to reset the password</p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Reset</button>
</form>
编辑
如果我将模板从注册文件夹移到模板文件夹中,则可以进行以下工作
path('logout/', auth_views.LogoutView.as_view(template_name='logout.html'), name='logout'),
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='password_reset_form.html'), name='password_reset'),
但是为什么之前不起作用
您的模板位于以下位置:
templates/registration/logout.html
templates/registration/password_reset_form.html
虽然您没有通过
urls.py
中的正确位置
您应该经过正确的位置:
path('logout/', TemplateView.as_view(template_name='registration/logout.html'), name='customlogout'),
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='registration/password_reset_form.html'), name='password_reset'),