我正在尝试为用户编写一个页面来更新他们的信息并更改他们的密码。
我现在可以工作了。我提供的代码有效,但我不知道如何告诉用户他们输入了错误的密码。如果用户输入密码,则表格有效,但如果他们输入错误的密码,我想说表格无效。但是,如果我将 forms.ValidationError("some error msg") 放在现在显示 forms.errors 的 else stmt 中,它不会返回模板,而是在编译器 msg 页面中显示错误 msg。
这是我从 django.contrib.auth.models 导入用户
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.')