0
我有一个问题,我觉得我之前解决,但由于某种原因,我只是无法弄清楚。我有一个Django视图,它将更新存储在数据库中的用户信息,该信息涉及用户输入其当前密码,新密码和验证新密码。如何通过重定向到当前页面向用户显示错误消息 - django
我已完成处理,用于输入所有正确信息的情况。我不想添加else语句,以防万一用户没有输入当前存在的密码或密码不匹配。
在else语句中的两条注释行中,我希望显示带有窗体的当前HTML模板,并且显示一条消息,显示为存在的不同错误分配的消息。
if 'passwordSubmit' in request.POST:
updatePassword = updatePasswordForm(request.POST)
if updatePassword.is_valid():
cd = updatePassword.cleaned_data
old_password = cd['old_password']
new_password = cd['new_password']
verify_password = cd['verify_password']
user = authenticate(username=currentUser.username, password=old_password)
if user is not None:
if new_password == verify_password:
secure_password = make_password(new_password)
update_user = currentUser
update_user.password = secured_password
update_user.save()
else:
message = 'The two passwords do not match'
# if the passwords do no match
else:
message = 'The password entered does not match our records'
# in case the old password is not what is saved in the database
在视图中,你可以通过渲染或许多方式返回一个变量,django提供了在模板中的访问 –
你可以使用Django的消息框架https://docs.djangoproject.com/en/1.11/ref/contrib/消息/ – Satendra