在Django密码中强制使用最小长度
我目前正在使用在Django密码中强制使用最小长度,django,django-forms,Django,Django Forms,我目前正在使用django.contrib.auth.views.password\u password\u reset\u confirm更改用户密码。以下是我的URL的外观: from django.contrib.auth import views as auth_views url(r'^password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', redirect_if_logged
django.contrib.auth.views.password\u password\u reset\u confirm
更改用户密码。以下是我的URL的外观:
from django.contrib.auth import views as auth_views
url(r'^password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
redirect_if_loggedin(auth_views.password_reset_confirm),
name='auth_password_reset_confirm'),
从django.contrib.auth导入视图作为auth\u视图
url(r'^password/reset/confirm/(?P[0-9A-Za-z]+)-(?P.+)/$”,
重定向日志(验证视图、密码重置确认),
name='auth\u password\u reset\u confirm',
目前,我正在直接将其放入django主干中-
# django.contrib.auth.views
def clean_new_password2(self):
password1 = self.cleaned_data.get('new_password1')
password2 = self.cleaned_data.get('new_password2')
if password1 and password2:
if len(password1) < 8:
raise forms.ValidationError(_("Password must be at least 8 chars."))
if password1 != password2:
raise forms.ValidationError(_("The two password fields didn't match."))
return password2
#django.contrib.auth.views
def clean_new_密码2(自身):
password1=self.cleaned\u data.get('new\u password1')
password2=self.cleaned\u data.get('new\u password2')
如果密码1和密码2:
如果len(密码1)<8:
raise forms.ValidationError((密码必须至少为8个字符)
如果密码为1!=密码2:
raise forms.ValidationError((两个密码字段不匹配)
返回密码2
肯定有更好的方法。如果我理解正确,您是在修改django代码吗?因为这样做是不可能的 你用什么表格?看起来内置的确实不允许您设置最小长度
也许你可以使用该视图设置你自己的密码更改表单,它可以从基本密码更改表单继承,你可以对其应用额外的设置。我使用的是django注册插件,我发现它非常优秀,因此我的示例就是基于此。但是没有它你也可以做类似的事情 很好地演示了如何覆盖django注册的表单(和小部件)——在本例中是针对recaptcha垃圾邮件机器人拦截器 您需要做的是覆盖RegistrationForm类(如下所示),并将URL.py指向使用它而不是默认的RegistrationForm
class MinPasswdLenRegistrationForm(RegistrationForm):
min_password_length = 8
def clean_password1(self):
" Minimum length "
password1 = self.cleaned_data.get('password1', '')
if len(password1) < self.min_password_length:
raise forms.ValidationError("Password must have at least %i characters" % self.min_password_length)
else:
return password1
HTH我在理解亚瑟的公认答案后最终编写的代码: 这是继承的形式:
class SetPasswordWithMinLengthForm(SetPasswordForm):
"""
Inherited form that lets a user change set his/her password without
entering the old password while validating min password length
"""
def clean_new_password1(self):
password1 = self.cleaned_data.get('new_password1')
if len(password1) < 4:
raise ValidationError("Password must be at least 4 chars.")
return password1
如果len(password1)接受长度为7(至少不是8)的密码,
会不会接受?答案很好,我用我最终编写的代码添加了另一个答案
class SetPasswordWithMinLengthForm(SetPasswordForm):
"""
Inherited form that lets a user change set his/her password without
entering the old password while validating min password length
"""
def clean_new_password1(self):
password1 = self.cleaned_data.get('new_password1')
if len(password1) < 4:
raise ValidationError("Password must be at least 4 chars.")
return password1
url(r'^forgot_password/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
'django.contrib.auth.views.password_reset_confirm',
{'set_password_form':SetPasswordWithMinLengthForm}),