Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中检查并确认相同的电子邮件_Python_Django_Django Forms_Django Validation - Fatal编程技术网

Python 如何在django中检查并确认相同的电子邮件

Python 如何在django中检查并确认相同的电子邮件,python,django,django-forms,django-validation,Python,Django,Django Forms,Django Validation,我有以下表格: class SignupForm(forms.ModelForm): time_zone = forms.ChoiceField(choices=TIMEZONE_CHOICES) email = forms.EmailField() confirm_email = forms.EmailField() password1 = forms.CharField(widget=forms.PasswordInput()) password2

我有以下表格:

class SignupForm(forms.ModelForm):
    time_zone = forms.ChoiceField(choices=TIMEZONE_CHOICES) 
    email = forms.EmailField()
    confirm_email = forms.EmailField()
    password1 = forms.CharField(widget=forms.PasswordInput())
    password2 = forms.CharField(widget=forms.PasswordInput())     

    class Meta:
        model = Customer
        fields = (
                  'first_name', 
                  'last_name',
                  'coupon_code'
                  )

    def clean_email(self):
        email = self.cleaned_data['email']
        print 'email cleaned data: '+cleaned_data
        try:
            User.objects.get(email=email)
            raise forms.ValidationError('Email already exists.')
        except User.DoesNotExist:
            return email

    def clean_confirm_email(self):
        print '-----'
        print 'confirm email cleaned data: '+cleaned_data
        email = self.cleaned_data['email']
        confirm_email = self.cleaned_data['confirm_email']
        if email != confirm_email:
            raise forms.ValidationError('Emails do not match.')
        return confirm_email
这张照片是:

email cleaned data: 
{'coupon_code': u'coup', 'first_name': u'Gina', 'last_name': u'Silv', 'time_zone': u'America/New_York', 'email': u'4email@example.net'}
-----
confirm email cleaned data: 
{'first_name': u'Gina', 'last_name': u'Silv', 'confirm_email': u'4email@example.net', 'time_zone': u'America/New_York', 'coupon_code': u'coup'}
运行此操作时,我得到错误信息:

key error 'email' self.cleaned_data['email']

如何访问
clean\u confirm\u email
方法中的电子邮件字段?

您不应该在
clean\u confirm\u email()方法中执行该验证。相反,请按照建议在
clean()
方法中执行此操作,如下所示:

def clean(self):
        cleaned_data = super(SignupForm, self).clean()
        email = cleaned_data.get("email")
        confirm_email = cleaned_data.get("confirm_email")

        if email and confirm_email:
        # Only do something if both fields are valid so far.
            if email != confirm_email:
                raise forms.ValidationError("Emails do not match.")

        return cleaned_data

此处的更多信息:

您不应该在
clean\u confirm\u email()
方法中执行该验证。相反,请按照建议在
clean()
方法中执行此操作,如下所示:

def clean(self):
        cleaned_data = super(SignupForm, self).clean()
        email = cleaned_data.get("email")
        confirm_email = cleaned_data.get("confirm_email")

        if email and confirm_email:
        # Only do something if both fields are valid so far.
            if email != confirm_email:
                raise forms.ValidationError("Emails do not match.")

        return cleaned_data

此处的更多信息:

您不应该在
clean\u confirm\u email()
方法中执行该验证。相反,请按照建议在
clean()
方法中执行此操作,如下所示:

def clean(self):
        cleaned_data = super(SignupForm, self).clean()
        email = cleaned_data.get("email")
        confirm_email = cleaned_data.get("confirm_email")

        if email and confirm_email:
        # Only do something if both fields are valid so far.
            if email != confirm_email:
                raise forms.ValidationError("Emails do not match.")

        return cleaned_data

此处的更多信息:

您不应该在
clean\u confirm\u email()
方法中执行该验证。相反,请按照建议在
clean()
方法中执行此操作,如下所示:

def clean(self):
        cleaned_data = super(SignupForm, self).clean()
        email = cleaned_data.get("email")
        confirm_email = cleaned_data.get("confirm_email")

        if email and confirm_email:
        # Only do something if both fields are valid so far.
            if email != confirm_email:
                raise forms.ValidationError("Emails do not match.")

        return cleaned_data
更多信息请点击此处: