【发布时间】:2009-12-09 05:21:19
【问题描述】:
我正在尝试验证一个表单(它以前可以工作)。出于某种原因,我似乎无法在调用 form.is_valid() 时调用诸如 clean_username(self) 之类的各种清理功能。
我知道还没有足够的检查(你看,它们正在建设中 ;-)),但这是我的课程:
class LoginForm(forms.Form):
username = forms.CharField(max_length=30)
password = forms.CharField(max_length=30,widget=forms.PasswordInput)
def clean_password(self):
print "Cleaning password"
password = self.cleaned_data['password']
if password == u"":
raise forms.ValidationError("Password is blank.")
return password
def clean_username(self):
username = self.cleaned_data['username']
if len(username) < 4:
raise forms.ValidationError("Username is fewer than four charecters.")
return username
class RegistrationForm( LoginForm ):
confirm_password = forms.CharField(widget=forms.PasswordInput, max_length=30)
email = forms.EmailField()
def clean_confirm_password(self):
print "Cleaning confirm password"
clean_confirm_password = self.cleaned_data['confirm_password']
if clean_confirm_password == u"":
raise forms.ValidationError("Confirming password is blank.")
return clean_confirm_password
def clean(self):
print "Running clean on a registration form"
print self.cleaned_data.items()
password = self.cleaned_data['password']
confirm = self.cleaned_data['confirm_password']
if password != confirm:
raise forms.ValidationError('Passwords do not match.')
return self.cleaned_data
谢谢!
【问题讨论】:
标签: django django-forms