说到正常的POST,GET方法,我通常都知道自己的方式.
但是,在我的代码中实现ajax-jQuery以进行表单验证是我学习的重要一步.
form.py
class UserField(forms.EmailField):
def clean(self,value):
super(UserField,self).clean(value)
try:
User.objects.get(username=value)
raise forms.ValidationError("email already taken.")
except User.DoesNotExist:
return value
class RegistrationForm(forms.Form):
email = UserField(max_length=30,required = True)
conf_email = UserField(label="Confirm Email",max_length=30,required = True)
password = forms.CharField(label="Enter New Password",widget=forms.PasswordInput(),required=True)
def clean(self):
if 'email' in self.cleaned_data and 'conf_email' in self.cleaned_data:
if self.cleaned_data['email'] != self.cleaned_data['conf_email']:
self._errors['email'] = [u'']
self._errors['conf_email'] = [u'Email must match.']
return self.cleaned_data
我希望在我按下提交按钮之前,通过使用ajax-jQuery方法检查表单是否有效并显示任何相关的错误消息.但是,我不知道如何开始/做这件事.
最佳答案