我已经覆盖了 Django 的内置password_mismatch错误以确认密码重置。在我的模板中,错误总是在加载后立即显示。密码重置过程运行顺利,但我什至无法摆脱在输入任何内容之前显示的错误。我怎样才能做到这一点?
我的代码如下所示。
表格.py
class PasswordResetConfirmationForm(SetPasswordForm):
new_password1 = CharField(max_length=35, required=True, widget=PasswordInput, error_messages={'required': 'Please do not leave blank.'})
new_password2 = CharField(max_length=35, required=True, widget=PasswordInput, error_messages={'required': 'Please do not leave blank.'})
error_messages = {
'password_mismatch': 'The two passwords should never match.',
}
视图.py
class PasswordResetConfirmationView(PasswordResetConfirmView):
form_class = PasswordResetConfirmationForm
def password_reset_confirm(self):
return render(self.request, 'accounts/registration/password_reset_confirm.html')
password_reset_confirm.html中的表格
<form method="post" novalidate="novalidate" class="popup-form empty-form">
{% csrf_token %}
<label for="{{ form.new_password1.id_for_label }}">Password</label>
{{ form.new_password1.errors }}
{% render_field form.new_password1 placeholder="New password" %}
<label for="{{ form.new_password2.id_for_label }}">Re-enter password</label>
<p>{{ form.error_messages.password_mismatch }}</p>
{% render_field form.new_password2 placeholder="New password again" %}
<input type="submit" value="Change password">
</form>