使用内置的Django身份验证模块时,如何设置最小密码长度? [英] How can I set a minimum password length when using the built-in Django auth module?

查看:106
本文介绍了使用内置的Django身份验证模块时,如何设置最小密码长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django网站中使用内置认证模块,包括内置的 UserCreationForm

I’m implementing authentication in a Django site using the built-in auth module, including the built-in UserCreationForm.

我想设置密码的最小长度。但是,我找不到有关如何执行此操作的任何文档。

I’d like to set a minimum length for passwords. However, I can’t find any documentation on how to do this.

我可以配置auth模块的用户模块要求在数据库级别?或者我应该将 UserCreationForm (我实际上是为了无关的原因)分类,并添加一个强制密码长度的额外的验证器?

Can I configure the auth module’s User module to require this at the database level? Or should I sub-class the UserCreationForm (I’m actually doing this already for unrelated reasons) and add an extra validator that enforces the password length?

推荐答案

特别是如果您已经在使用子类 UserCreationForm ,我会说你肯定只是添加验证。您应该可以覆盖表单上的 clean_password 方法:

Especially if you're already using a sub-classed UserCreationForm, I'd say you should definitely just add the validation to it. You should be able to override the clean_password method on the form:

def clean_password(self):
    password = self.cleaned_data.get('password1')
    if len(password) < 8:
        raise ValidationError('Password too short')
    return super(MyUserCreationForm, self).clean_password1()

这篇关于使用内置的Django身份验证模块时,如何设置最小密码长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆