我在尝试访问帖子数据的'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"))