How can I set a minimum password length when using the built-in Django auth module?

前端 未结 5 891
南方客
南方客 2021-01-01 21:25

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 passwo

5条回答
  •  猫巷女王i
    2021-01-01 21:47

    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()
    

提交回复
热议问题