Why is there a key error in this line of my Django forms code?
class RegisterForm(forms.Form):
username = forms.CharField(max_length=16, label="Username", required=False)
password = forms.CharField(max_length=100,widget=forms.PasswordInput, require开发者_JAVA技巧d=False)
password2 = forms.CharField(max_length=100,widget=forms.PasswordInput, label="Password Again", required=False)
fullname = forms.CharField(max_length = 100, required=False)
email = forms.EmailField(max_length=100, required=False)
def clean_fullname(self):
if len(self.cleaned_data['fullname']) < 4:
raise forms.ValidationError("Enter your full name.")
def clean_email(self):
if self.cleaned_data['email'].find("@") <= 0:
raise forms.ValidationError("Enter a valid email address.")
def clean_username(self):
if not self.cleaned_data['username']:
raise forms.ValidationError("Enter a username.")
try:
u = User.objects.get(username = self.cleaned_data['username'])
if u:
raise forms.ValidationError("Username is taken.")
except:
pass
def clean_password(self):
if not self.cleaned_data['password']:
raise forms.ValidationError("Enter a password.")
def clean_password2(self):
if not self.cleaned_data['password2']:
raise forms.ValidationError("Enter your password (again)")
def clean(self):
cleaned_data = self.cleaned_data
password = cleaned_data['password'] <<< There is a key error here anytime I submit a form.
password2 = cleaned_data['password2']
if password and password2:
if password != password2:
raise forms.ValidationError("Your passwords do not match.")
return cleaned_data
There's a key error on password = cleaned_data['password']
The reason is because your clean_password
function doesn't return anything. Neither does your clean_password2
This is the order of validation:
- clean_fieldnames
- clean
clean_password
and clean_password2
need to return a value when they are valid.
def clean_password(self):
if not self.cleaned_data['password']:
raise forms.ValidationError("Enter a password.")
return self.cleaned_data['password']
def clean_password2(self):
if not self.cleaned_data['password2']:
raise forms.ValidationError("Enter your password (again)")
return self.cleaned_data['password2']
although the recommended format to save a few keystrokes (especially on longer validation) is...
def clean_password2(self):
data = self.cleaned_data['password2']
raise forms.ValidationError("Enter your password (again)")
return data
Well the short literal answer is that self.cleaned_data is a dict that has no entry with 'password' as a key. Going to the next stage:
Have you tried inspecting self.cleaned_data
? Is it missing only a password
entry or is it empty? Did you type in a password on the form? What do you expect to be in self.cleaned_data if nothing is typed into the form? What is the basis of your expectation?
I consider it rather suspect that a method whose name is an action verb like "clean" starts working on an attribute named "cleaned_data" instead of "raw_data" or "uncleaned_data" or "unclean_data" or "not_known_to_be_clean_data" ... is this your idea or just Django weirdness?
精彩评论