显示用户输入错误密码的错误消息
我正在尝试为用户编写一个页面来更新他们的信息并更改他们的密码。
我现在可以使用了。我提供的代码有效,但我不知道如何告诉用户他们输入了错误的密码。如果用户输入密码,则表单有效,但如果他们输入错误的密码,我想说该表单无效。但是,如果我将 forms.ValidationError("some error msg") 放在现在显示 forms.errors 的 else stmt 中,它不会返回到模板,而是在编译器消息页面中显示错误消息。
这是我的 from django.contrib.auth.models import User
def edit_page(request):
u = request.user
if request.method == 'POST':
form = EditUserForm(request.POST)
if form.is_valid():
if u.check_password(form.cleaned_data['oldPassword']):
u.set_password(form.cleaned_data['password1'])
u.save()
return HttpResponseRedirect('/')
else:
form.errors //Where I put forms.ValidationError()
else:
form = EditUserForm()
variables = RequestContext(request, {
'form': form, 'user': request.user
})
return render_to_response(
'registration/Edit_User.html',
variables
)
这是在我的 forms.pyclass 中
EditUserForm(forms.Form):
oldPassword = forms.CharField(
label=u'Current Password',
widget = forms.PasswordInput()
)
password1 = forms.CharField(
label=u'New Password',
widget=forms.PasswordInput()
)
password2 = forms.CharField(
label=u'New Password (Again)',
widget=forms.PasswordInput()
)
def clean_password2(self):
if 'password1' in self.cleaned_data:
password1 = self.cleaned_data['password1']
password2 = self.cleaned_data['password2']
if password1 == password2:
return password2
raise forms.ValidationError('Passwords do not match.')
I am trying to write a page for a user to update their information and change their password.
I have it working now. The code that I have provided works, but I do not know how to tell the user they entered the wrong password. If the user enters a password the form is valid but if they enter the wrong password I want to say the form is not valid. But if I put forms.ValidationError("some error msg") in the else stmt that right now says forms.errors, it doesn't go back to the template but displays the error msg in a compiler msg page.
This is my
from django.contrib.auth.models import User
def edit_page(request):
u = request.user
if request.method == 'POST':
form = EditUserForm(request.POST)
if form.is_valid():
if u.check_password(form.cleaned_data['oldPassword']):
u.set_password(form.cleaned_data['password1'])
u.save()
return HttpResponseRedirect('/')
else:
form.errors //Where I put forms.ValidationError()
else:
form = EditUserForm()
variables = RequestContext(request, {
'form': form, 'user': request.user
})
return render_to_response(
'registration/Edit_User.html',
variables
)
This is in my forms.pyclass
EditUserForm(forms.Form):
oldPassword = forms.CharField(
label=u'Current Password',
widget = forms.PasswordInput()
)
password1 = forms.CharField(
label=u'New Password',
widget=forms.PasswordInput()
)
password2 = forms.CharField(
label=u'New Password (Again)',
widget=forms.PasswordInput()
)
def clean_password2(self):
if 'password1' in self.cleaned_data:
password1 = self.cleaned_data['password1']
password2 = self.cleaned_data['password2']
if password1 == password2:
return password2
raise forms.ValidationError('Passwords do not match.')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在您的
clean_oldPassword
方法中。您在字符串“oldPassword”上调用check_password
,而不是变量,所以自然会失败。您还应该确保您的模板显示
form.errors
,以便您了解其未验证的原因。The problem is in your
clean_oldPassword
method. You callcheck_password
on the string 'oldPassword', instead of the variable, so naturally it fails.You should also ensure that your template displays
form.errors
, so you can see why it doesn't validate.