Python Django:内置视图和表单;对象没有属性';用户缓存'&引用;
正在尝试使用内置的auth.views和auth.form重置密码 在Python Django:内置视图和表单;对象没有属性';用户缓存'&引用;,python,django,forms,Python,Django,Forms,正在尝试使用内置的auth.views和auth.form重置密码 在forms.py中包含以下内容: class CustomPasswordResetForm(PasswordResetForm): def clean_email(self): email = self.cleaned_data.get('email') if email and 'gmail.com' in email: raise forms.ValidationError
forms.py中包含以下内容:
class CustomPasswordResetForm(PasswordResetForm):
def clean_email(self):
email = self.cleaned_data.get('email')
if email and 'gmail.com' in email:
raise forms.ValidationError(u'Unfortunately, we can not reset Gmail usernames')
return email
url(r'^passreset/$', auth_views.password_reset, {'template_name': 'registration/password_reset.html', 'password_reset_form': CustomPasswordResetForm}),
以及url.py中的以下内容:
class CustomPasswordResetForm(PasswordResetForm):
def clean_email(self):
email = self.cleaned_data.get('email')
if email and 'gmail.com' in email:
raise forms.ValidationError(u'Unfortunately, we can not reset Gmail usernames')
return email
url(r'^passreset/$', auth_views.password_reset, {'template_name': 'registration/password_reset.html', 'password_reset_form': CustomPasswordResetForm}),
如果我像sample@gmail.com
,它会正确地抛出验证错误。但是,如果我键入一封非gmail电子邮件,它会验证它在超类中是否是正确的电子邮件地址,但不会验证是否有用户与该电子邮件关联,而是返回:
AttributeError at /passreset/
'CustomPasswordResetForm' object has no attribute 'users_cache'
有什么想法吗?我不知道从哪里开始,谷歌搜索也没有找到多少结果。解决了这个问题。一旦我重写了clean_email,我就忘了调用超类中的原始方法。将第3行更改为下面可以解决此问题
email = super(CustomPasswordResetForm, self).clean_email()