在Django密码中强制设置最小长度

7

我目前正在使用django.contrib.auth.views.password_password_reset_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主干上进行开发 -

# 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

当然,一定有更好的方法。

if len(password1) < 7 不会接受长度为7的密码(不足8个字符),对吗? - Dirk
3个回答

6
我将根据Arthur的答案,提供下述代码翻译:

以下是继承的表单代码:

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

urls.py文件中,您可以通过指定set_password_form来指示视图使用定制表单:
url(r'^forgot_password/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
                                                     'django.contrib.auth.views.password_reset_confirm',
                                                     {'set_password_form':SetPasswordWithMinLengthForm}),

4
我正在使用django-registration插件,我发现它非常优秀,因此我的示例基于该插件。但是,即使没有它,你也可以做类似的事情。 这篇文章很好地指导如何覆盖django-registration的表单(和小部件),在这种情况下,用于防止recaptcha垃圾邮件机器人。
你需要做的就是覆盖RegistrationForm类(如下所示),并将urls.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

在一个表单类中,Django会查找以clean_开头且以字段名结尾(如password1)的函数,在表单验证期间执行。
另一个重要的部分是在urls.py中使用表单,如下所示:
from django.conf.urls.defaults import *
from registration.views import register

from myapp.forms import MinPasswdLenRegistrationForm

urlpatterns = patterns('',
    url(r'^register/$', register,
        {'form_class': MinPasswdLenRegistrationForm},
        name='registration.views.register'),
    (r'', include('registration.urls')),
)

HTH


2
如果我理解正确的话,您是在修改Django代码吗?因为这绝对不是正确的做法。
您使用什么表单?似乎内置的PasswordChangeForm无法让您设置最小长度。
也许您可以使用password_change视图并设置自己的password_change_form,它可以继承基本的PasswordChangeForm,然后您可以应用额外的cleaning

很好的答案,我已经添加了另一个答案,其中包含我最终编写的代码 https://dev59.com/SF_Va4cB1Zd3GeqPUIUR#20678355 - Variant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接