【发布时间】:2020-03-20 11:56:07
【问题描述】:
我是 PasswordChangeForm 表单来更改密码。当给定密码小于 8 或大于 64(在下面的代码“form.data ['new_password1']”中)时,我试图显示一条错误消息。因此,当我输入少于 8 个字符的密码时,我会看到错误消息“新密码应包含最少 8 个字符和最多 64 个字符”命中。但错误信息未显示在 UI 页面上。这是因为 "return render(request, 'registration/change_password.html'" 再次渲染。
请您帮我看看如何在 PasswordChangeForm 上显示错误消息。
@login_required(login_url='/login/')
def change_password_view(request):
global passwordValidationFailed
passwordValidationFailed = False
if (request.method == 'POST'):
form = PasswordChangeForm(request.user, request.POST)
if len(form.data['new_password1']) >= 8 and len(form.data['new_password1']) <= 64:
if form.is_valid():
form.save()
messages.success(request, 'Your password was successfully updated!')
profile = request.user.get_profile()
profile.force_password_change = False
profile.save()
return render(request, 'dau_gui_app/status_view.html', {'title': "System Status"})
else:
passwordValidationFailed = False
messages.error(request, 'Please correct the error below.')
else:
raise form.ValidationError("New password should have minimum 8 characters and maximum 64 characters")
else:
form = PasswordChangeForm(request.user)
return render(request, 'registration/change_password.html', {
'form': form
})
这是我的 change_password.html
{% load i18n %}
{% load admin_static %}{% load firstof from future %}<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="static/dau_gui_app/fontawesome-free-5.3.1-web/css/all.min.css">
<link rel='shortcut icon' type='image/x-icon' href='/static/dau_gui_app/images/favicon.ico' />
<link rel="stylesheet" href="/static/dau_gui_app/style.css">
<link rel="stylesheet" href="/static/dau_gui_app/bootstrap.min.css">
<link rel="stylesheet" href="/static/dau_gui_app/w3.css">
<link rel="stylesheet" href="/static/dau_gui_app/dataTables/datatables.css">
<script type="text/javascript" src="/static/dau_gui_app/dataTables/jQuery-3.3.1/jquery-3.3.1.js"></script>
<script type="text/javascript" src="/static/dau_gui_app/dataTables/datatables.js"></script>
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static 'admin/css/base.css' %}{% endblock %}" />
{% block extrastyle %}{% endblock %}
<title>
{% block title %}Change Password{% endblock %}
</title>
</head>
{% load i18n %}
<body id="login_page">
<DIV style="margin-left:auto;margin-right:auto;padding-top:100px;display:block;width:30%">
<DIV id="login_box">
<table id="logon_box_table" >
<!-- Title Bar -->
<TR >
<TD colspan='2' >
<DIV id='login_box_title'>
Update Password
</DIV>
</TD>
</TR>
<!-- Password -->
<TR >
<TD >
<div id= "logon-container" >
<img src="/static/dau_gui_app/images/logo.png" alt="">
<div id="alstom-logo-container"> <img src="/static/dau_gui_app/images/alstom_logo.png" alt="" style="width: 90px; height: 18px;"> </div>
<div id="version-container" >Software Version: 4.0</div>
</div>
</TD>
<TD style="width: 490px; height: 18px;">
{% block content %}
<form method="post">
{% csrf_token %}
{{ form }}
</TD>
</TR>
<TR >
<TR >
<TD colspan='2' style="text-align:center;padding:10px">
<button type="submit">Update Password</button>
</form>
{% endblock %}
</TD>
</TR>
</TABLE>
</DIV>
</DIV>
</body>
</html>
【问题讨论】:
-
您不应在视图中执行验证。改写表单中的
clean()或clean_new_password1()方法,如here 所述。这样,您提出的任何错误都将添加到表单的错误中。
标签: django django-models django-forms django-views