Displaying error msg that the user entered the wrong password
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开发者_StackOverflow社区 = 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.')
The problem is in your clean_oldPassword
method. You call check_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.
精彩评论