Tôi đang cố gắng xác thực biểu mẫu (và nó đã từng hoạt động trước đó). Đối với một số lý do, tôi dường như không thể có được các chức năng làm sạch khác nhau như clean_username (self) để gọi khi form.is_valid() được gọi.Biểu mẫu Django không gọi clean_ <fieldname>
Tôi biết có không phải là gần đủ kiểm tra nêu ra (họ đang được xây dựng mà bạn nhìn thấy ;-)), nhưng đây là lớp học của tôi:
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
Cảm ơn bạn!
1 cho đào vào forms.py. Nó làm tôi ngạc nhiên như thế nào mọi người sẽ không nhìn vào mã nguồn sẵn có. – cethegeek
Nó có thể đáng sợ. Tôi sợ vô tình thay đổi và tiết kiệm một cái gì đó quan trọng. Tiềm lực, tôi biết khi nào toàn bộ vật có thể được thay thế dễ dàng. ;-) – SapphireSun