Django clean删除一个输入字段

时间:2014-06-11 17:40:42

标签: django forms dictionary

我在尝试访问帖子数据的'email_password'部分时遇到了一个关键错误。在某些时候,cleaning_data会生成一个没有'email_password'的字典。我该如何解决这个问题?

class EmailForm(forms.Form):
    email = forms.EmailField(required=True)
    pword = forms.CharField(
        label=_('Old Password'),
        required=True,
        widget=forms.PasswordInput,
    )

    def __init__(self, *args, **kwargs):
        request = kwargs.pop('request', None)
        self.user = request.user
        return super(EmailForm, self).__init__(*args, **kwargs)

    def clean(self, *args, **kwargs):
        cleaned_data = super(EmailForm, self).clean()
        data = cleaned_data['email']
        pword = cleaned_data['email_password']
        try:
            User.objects.exclude(id=self.user.id).get(email=data)
        except User.DoesNotExist:
            return cleaned_data
        else:
            raise forms.ValidationError(_("Email is already been used by another user"))

0 个答案:

没有答案