重定向后添加表单。如果表单无效,则发送错误消息。模板
中没有出现错误@login_required(login_url="login")
@user_passes_test(user_is_patient_check, login_url='login')
def changePasswordPatient(request):
if request.method == "POST":
form = PasswordChangeForm(request.user,request.POST)
if form.is_valid():
user=form.save()
update_session_auth_hash(request, user)
messages.success(request,'Şifreniz başarıyla güncellendi!')
return redirect("changePasswordPatient")
else:
messages.error(request,form.errors,extra_tags="invalidchangepassword")
return redirect("changePasswordPatient") # this part
form=PasswordChangeForm(request.user)
context={
"form":form,
"which_active":"passwordchange"
}
return render(request,"change-password.html",context)
,但当我改变它的形式不有效的部分,像使用这个(渲染方法)。模板中显示表单错误。但在这个方法中当我刷新页面仍然显示错误消息。谁能帮我解决这个问题?
if form.is_valid():
user=form.save()
update_session_auth_hash(request, user) # Important!
messages.success(request,'Şifreniz başarıyla güncellendi!')
return redirect("changePasswordPatient")
else:
messages.error(request,form.errors,extra_tags="invalidchangepassword")
return render(request,"change-password.html",{"form":form,"which_active":"passwordchange"})
change-password.html
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<center><strong>{{ error|escape }}</strong></center>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<center><strong>{{ error|escape }}</strong></center>
</div>
{% endfor %}
{% endif %}
您可以将您的redirect
编辑为以下内容:
messages.error(request,form.errors,extra_tags="invalidchangepassword")
form=PasswordChangeForm(request.user)
return redirect("changePasswordPatient", {"form":form}) # this part