Python 在Django中使用内置重置密码和密码更改时,如何显示错误消息
我正在使用Django内置功能处理一些用户忘记密码以及密码重置的问题,但我正在使用我的模板来呈现页面和表单。我想用一种方法来呈现错误或成功消息,以便用户在密码太短的情况下了解发生了什么 url.pyPython 在Django中使用内置重置密码和密码更改时,如何显示错误消息,python,django,django-views,django-forms,django-admin,Python,Django,Django Views,Django Forms,Django Admin,我正在使用Django内置功能处理一些用户忘记密码以及密码重置的问题,但我正在使用我的模板来呈现页面和表单。我想用一种方法来呈现错误或成功消息,以便用户在密码太短的情况下了解发生了什么 url.py from django.urls import path from . import views from django.contrib.auth import views as auth_views urlpatterns = [ path('', views.Dashboard, na
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', views.Dashboard, name='dashboard'),
path('login/', views.Login, name='login'),
path('logout/', views.Logout, name='logout'),
path('register/', views.Register, name='register'),
path('forgetpassword/', views.ForgetPassword, name='forgetpassword'),
path('password_change/', auth_views.PasswordChangeView.as_view(
template_name='auth/password/change-password.html'), name='password_change'),
path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(
template_name='auth/password/change-password-done.html'), name='password_change_done'),
change-password.html
{% extends 'auth/base.html' %}
{% load static %}
{% block content %}
<div class="page-wrapper" style="height: 100vh!important;">
<div class="page-content--bge5">
<div class="container">
<div class="login-wrap">
<div class="login-content">
<div class="login-logo">
<a href="#">
<img src="{% static 'images/icon/logo-new.png' %}" alt="CoolAdmin">
</a>
</div>
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{message.tags}} with-close alert-dismissible fade show">
{{message}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
{% endif %}
<div class="login-form">
<form method="POST">
{% csrf_token %}
<div class="form-group">
<label>Current Password</label>
<input class="au-input au-input--full" type="password" name="old_password" id="id_old_password" placeholder="Current Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password1" id="id_new_password1" placeholder="New Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password2" id="id_new_password2" placeholder="New Password" required>
</div>
<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">Change Password</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{%extends'auth/base.html%}
{%load static%}
{%block content%}
{%if消息%}
{消息%中的消息为%s}
{{message}}
&时代;
{%endfor%}
{%endif%}
{%csrf_令牌%}
当前密码
新密码
新密码
修改密码
{%endblock%}
表单的实例中已经存在错误。出于某种原因,人们使用消息框架来显示错误,这实际上比简单地在模板中使用表单更困难。视图将表单实例传递给模板,以便您可以使用它渲染表单并渲染其错误。在模板中尝试以下操作:
要呈现非字段错误,请执行以下操作:
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
要呈现字段错误,请使用form.field\u name.errors
:
{% for error in form.new_password1.errors %}
{{ error }}
{% endfor %}
需要注意的是,手动呈现表单(通过自己编写标记)会增加无正当理由生成表单的难度。也许有人不喜欢使用默认呈现的表单,但是有一些方法可以在表单的类中或使用一些包来定制这些表单。请参阅和两个伟大的定制表单呈现的软件包。我按照前面的答案解决了这个问题。我在HTML中添加了错误标记,这使它工作得非常完美,并使用一些样式很好地显示了错误
{%for form.old_password.errors%}
{{error}}
&时代;
{%endfor%}
{%for form.new_password1.errors%}
{{error}}
&时代;
{%endfor%}
{%for form.new_password2.errors%}
{{error}}
&时代;
{%endfor%}
{%csrf_令牌%}
当前密码
新密码
新密码
修改密码