Django 密码重置表单,自己清理方法
我想为密码重置表单编写自己的方法,对新密码(长度、字符等)进行一些测试。所以我将这个类添加到我的forms.py中:Django 密码重置表单,自己清理方法,django,django-forms,Django,Django Forms,我想为密码重置表单编写自己的方法,对新密码(长度、字符等)进行一些测试。所以我将这个类添加到我的forms.py中: class PasswordResetForm(SetPasswordForm): def clean(self): if 'newpassword1' in self.cleaned_data and 'newpassword2' in self.cleaned_data: if self.cleaned_data['newpassword1'] !=
class PasswordResetForm(SetPasswordForm):
def clean(self):
if 'newpassword1' in self.cleaned_data and 'newpassword2' in self.cleaned_data:
if self.cleaned_data['newpassword1'] != self.cleaned_data['newpassword2']:
raise forms.ValidationError(("The two password fields didn't match."))
#here the passwords entered are the same
if len(self.cleaned_data['newpassword1']) < 8:
raise forms.ValidationError(("Your password has to be at least 8 characters long"))
if not re.search('\d+', self.cleaned_data['newpassword1']) or not re.search('([a-zA-Z])+', self.cleaned_data['new password1']):
raise forms.ValidationError(("Your password needs to contain at least one number and one character"))
return self.cleaned_data
class PasswordResetForm(SetPasswordForm):
def清洁(自清洁):
如果self.cleaned_数据中的“newpassword1”和self.cleaned_数据中的“newpassword2”:
如果自行清理_数据['newpassword1']!=self.cleanned_数据['newpassword2']:
raise forms.ValidationError((“两个密码字段不匹配。”)
#此处输入的密码相同
如果len(自清理_数据['newpassword1'])小于8:
raise forms.ValidationError((“您的密码长度必须至少为8个字符”))
如果未重新搜索('\d+',自清理_数据['newpassword1']),或未重新搜索('([a-zA-Z])+',自清理_数据['newpassword1']):
raise forms.ValidationError((“您的密码需要至少包含一个数字和一个字符”))
返回自清理的_数据
在URL.py中,我添加了以下内容:
url(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', django.contrib.auth.views.password_reset_confirm, {'template_name':'registration/password_reset_confirm.html',
'set_password_form': PasswordResetForm})
url(r'^reset/(?P[0-9A-Za-z]+)-(?P.+)/$”,django.contrib.auth.views.password\u reset\u confirm,{'template\u name':'registration/password\u reset\u confirm.html',
'set_password_form':PasswordResetForm})
但是我自己的clean方法没有被调用。这有什么问题?这个答案可能看起来很愚蠢,我没有理由,但我遇到了同样的问题,我通过替换
self.cleaned_data['newpassword1']
借
对于所有clean_数据集:
class PasswordResetForm(SetPasswordForm):
def clean(self):
cleaned_data = self.cleaned_data
if 'newpassword1' in cleaned_data and 'newpassword2' in cleaned_data:
if cleaned_data.get('newpassword1') != cleaned_data.get('newpassword2'):
raise forms.ValidationError(("The two password fields didn't match."))
if len(cleaned_data.get('newpassword1')) < 8:
raise forms.ValidationError(("Your password has to be at least 8 characters long"))
if not re.search('\d+', cleaned_data.get('newpassword1')) or not re.search('([a-zA-Z])+', cleaned_data.get('new password1')):
raise forms.ValidationError(("Your password needs to contain at least one number and one character"))
return cleaned_data
class PasswordResetForm(SetPasswordForm):
def清洁(自清洁):
已清理的\u数据=自清理的\u数据
如果已清理的\u数据中的“newpassword1”和已清理的\u数据中的“newpassword2”:
如果已清除数据,则获取('newpassword1')!=已清除数据。获取('newpassword2'):
raise forms.ValidationError((“两个密码字段不匹配。”)
如果len(cleaned_data.get('newpassword1'))小于8:
raise forms.ValidationError((“您的密码长度必须至少为8个字符”))
如果未重新搜索('\d+',清除的_数据.get('newpassword1'))或未重新搜索('([a-zA-Z])+',清除的_数据.get('newpassword1'):
raise forms.ValidationError((“您的密码需要至少包含一个数字和一个字符”))
返回已清除的数据
我发现了错误。这是我的URL.py中的一个错误。它必须是:
url(r'^password/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',django.contrib.auth.views.password_reset_confirm,{'template_name':'registration/password_reset_confirm.html','set_password_form': PasswordResetForm})
url(r'^password/reset/(?P[0-9A-Za-z]+)-(?P.+)/$),django.contrib.auth.views.password\u reset\u confirm,{'template\u name':'registration/password\u reset\u confirm.html','set\u password\u form':PasswordResetForm})
我不知道为什么我还有密码重置表而不是404。也许有人能告诉我。我有点担心仍然有可能使用标准重置表单。不是为我做的。还是一样的问题。当我为clean函数设置断点时,它不会被调用。因此,问题必须在别处。
url(r'^password/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',django.contrib.auth.views.password_reset_confirm,{'template_name':'registration/password_reset_confirm.html','set_password_form': PasswordResetForm})