我的表格看起来像这样。
class MyForm(Form):
first_name = forms.CharField(max_length=128,
widget=forms.TextInput(attrs={
'placeholder': 'First Name',
'class': "form-control"}),)
last_name = forms.CharField(max_length=128,
widget=forms.TextInput(attrs={
'placeholder': 'Last Name',
'class': "form-control"}),)
email = forms.EmailField(max_length=128,
widget=forms.TextInput(attrs={
'placeholder': 'Email',
'class': "form-control"}),)
confirm_email = forms.EmailField(max_length=128,
widget=forms.TextInput(attrs={
'placeholder': 'Confirm Email',
'class': "form-control"}),)
password = forms.CharField(max_length=128,
widget=forms.PasswordInput(attrs={
'placeholder': 'Password',
'class': "form-control"}),)
confirm_password = forms.CharField(max_length=128,
widget=forms.PasswordInput(attrs={
'placeholder': 'Confirm Password',
'class': "form-control"}),)
def clean_email(self):
cleaned_data = super(MyForm, self).clean()
email = cleaned_data.get('email')
confirm_email = cleaned_data.get('confirm_email')
if not confirm_email:
raise forms.ValidationError("Please confirm your email.")
if email and confirm_email:
if email != confirm_email:
raise forms.ValidationError("Emails do not match")
return confirm_email
def clean_password(self):
cleaned_data = super(MyForm, self).clean()
pwd = cleaned_data.get('password')
cpwd = cleaned_data.get('confirm_password')
if not cpwd:
raise forms.ValidationError("Please Confirm your Password")
if pwd and cpwd:
if pwd != cpwd:
raise forms.ValidationError("Passwords do not match.") #CNone
return cpwd
def clean(self, *args, **kwargs):
self.clean_email()
self.clean_password()
return super(MyForm, self).clean()
confirm_email
和confirm_passwor
d字段都返回None
,无法验证字段。我几乎不知道造成这个问题的原因。
我的表单存在问题,或者更有可能是视图甚至是模板的问题?
答案 0 :(得分:1)
class MyForm(Form):
first_name = forms.CharField(
max_length=128,
widget=forms.TextInput(
attrs={'placeholder': 'First Name','class': "form-control"}
),
)
last_name = forms.CharField(
max_length=128,
widget=forms.TextInput(
attrs={'placeholder': 'Last Name', 'class': "form-control"}
),
)
email = forms.EmailField(
max_length=128,
widget=forms.TextInput(
attrs={'placeholder': 'Email', 'class': "form-control"}
),
)
confirm_email = forms.EmailField(
max_length=128,
widget=forms.TextInput(
attrs={'placeholder': 'Confirm Email', n'class': "form-control"}
),
)
password = forms.CharField(
max_length=128,
widget=forms.PasswordInput(
attrs={'placeholder': 'Password', 'class': "form-control"}
),
)
confirm_password = forms.CharField(
max_length=128,
widget=forms.PasswordInput(
attrs={'placeholder': 'Confirm Password', 'class': "form-control"}
),
)
def clean_email(self):
cdata = self.cleaned_data
email = cdata['email']
confirm_email = cdata.['confirm_email']
if email and confirm_email:
if email != confirm_email:
raise forms.ValidationError("Emails do not match")
return email
def clean_confirm_email(self):
cdata = self.cleaned_data
confirm_email = cdata['confirm_email']
if not confirm_email:
raise forms.ValidationError("Please confirm your email.")
return confirm_email
def clean_password(self):
cdata = self.cleaned_data
password = cdata['password']
confirm_password = cdata['confirm_password']
if password and confirm_password:
raise forms.ValidationError("Passwords do not match.")
return password
def clean_confirm_password(self):
cdata = self.cleaned_data
confirm_password = cdata['confirm_password']
if not confirm_password:
raise forms.ValidationError("Please Confirm your Password")
return confirm_password