<small id='W4NOg'></small><noframes id='W4NOg'>

<tfoot id='W4NOg'></tfoot>

    <i id='W4NOg'><tr id='W4NOg'><dt id='W4NOg'><q id='W4NOg'><span id='W4NOg'><b id='W4NOg'><form id='W4NOg'><ins id='W4NOg'></ins><ul id='W4NOg'></ul><sub id='W4NOg'></sub></form><legend id='W4NOg'></legend><bdo id='W4NOg'><pre id='W4NOg'><center id='W4NOg'></center></pre></bdo></b><th id='W4NOg'></th></span></q></dt></tr></i><div id='W4NOg'><tfoot id='W4NOg'></tfoot><dl id='W4NOg'><fieldset id='W4NOg'></fieldset></dl></div>
  1. <legend id='W4NOg'><style id='W4NOg'><dir id='W4NOg'><q id='W4NOg'></q></dir></style></legend>
      <bdo id='W4NOg'></bdo><ul id='W4NOg'></ul>

    1. 如何在 django auth 密码验证器旁边使用自定义密码验证器?

      How to use custom password validators beside the django auth password validators?(如何在 django auth 密码验证器旁边使用自定义密码验证器?)
    2. <small id='tE73I'></small><noframes id='tE73I'>

          <tbody id='tE73I'></tbody>
        <legend id='tE73I'><style id='tE73I'><dir id='tE73I'><q id='tE73I'></q></dir></style></legend>
        <tfoot id='tE73I'></tfoot>

              <bdo id='tE73I'></bdo><ul id='tE73I'></ul>
              • <i id='tE73I'><tr id='tE73I'><dt id='tE73I'><q id='tE73I'><span id='tE73I'><b id='tE73I'><form id='tE73I'><ins id='tE73I'></ins><ul id='tE73I'></ul><sub id='tE73I'></sub></form><legend id='tE73I'></legend><bdo id='tE73I'><pre id='tE73I'><center id='tE73I'></center></pre></bdo></b><th id='tE73I'></th></span></q></dt></tr></i><div id='tE73I'><tfoot id='tE73I'></tfoot><dl id='tE73I'><fieldset id='tE73I'></fieldset></dl></div>

                本文介绍了如何在 django auth 密码验证器旁边使用自定义密码验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                正如上面提到的问题,我将尝试在注册过程中使用某个额外的规则来验证密码.额外的规则应该是验证密码是否至少包含一个数字、一个字母和一个特殊字符.

                As the question above mentioned, I will trying to use a certain extra rule to validate a password during the registration process. The extra rule should be that a password is validate if it has at least one digit, one letter and one special character.

                我解决这个问题的方法我创建了一个名为validators.py 的文件.

                My approach to solute this problem I've created a file named validators.py.

                from django.core.exceptions import ValidationError
                
                class CustomPasswortValidator:
                
                    def validate(value):
                
                      # check for digit
                      if not any(char.isdigit() for char in value):
                          raise ValidationError(_('Password must contain at least 1 digit.'))
                
                      # check for letter
                      if not any(char.isalpha() for char in value):
                          raise ValidationError(_('Password must contain at least 1 letter.'))
                
                      # check for special character
                      special_characters = "[~!@#$%^&*()_+{}":;'[]]"
                      if not any(char in special_characters for char in value):
                        raise ValidationError(_('Password must contain at least 1 letter.'))
                

                我的自定义注册表如下所示:

                My custom registration form looks like this:

                from django import forms
                from django.contrib.auth.forms import UserCreationForm
                from django.contrib.auth.models import User
                
                
                class RegistrationForm(UserCreationForm):
                    first_name = forms.CharField(max_length=30, required=False,                                         
                                                 help_text='Optional.')
                    last_name = forms.CharField(max_length=30, required=False, 
                                                 help_text='Optional.')
                    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
                
                    class Meta:
                        model = User
                        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )
                

                我不明白我是如何告诉 django,我的自定义密码验证器应该在 django AUTH_PASSWORD_VALIDATORS 旁边使用.

                I don't get it how I tell django, that my custom password validator should be use beside the django AUTH_PASSWORD_VALIDATORS.

                推荐答案

                所以,正如 e4c5 提到的,它真的很简单明了.

                So, as e4c5 mentioned, it is really simple and straightforward.

                我的 CustomPasswordValidator 看起来像这样:

                My CustomPasswordValidator looks like this:

                from django.core.exceptions import ValidationError
                from django.utils.translation import ugettext as _
                
                class CustomPasswordValidator():
                
                    def __init__(self, min_length=1):
                        self.min_length = min_length
                
                    def validate(self, password, user=None):
                        special_characters = "[~!@#$%^&*()_+{}":;'[]]"
                        if not any(char.isdigit() for char in password):
                            raise ValidationError(_('Password must contain at least %(min_length)d digit.') % {'min_length': self.min_length})
                        if not any(char.isalpha() for char in password):
                            raise ValidationError(_('Password must contain at least %(min_length)d letter.') % {'min_length': self.min_length})
                        if not any(char in special_characters for char in password):
                            raise ValidationError(_('Password must contain at least %(min_length)d special character.') % {'min_length': self.min_length})
                
                    def get_help_text(self):
                        return ""
                

                只需将其添加到 settings.py 中的 AUTH_PASSWORD_VALIDATORS 列表即可!

                Just add it to the list of AUTH_PASSWORD_VALIDATORS in settings.py and that's it!

                AUTH_PASSWORD_VALIDATORS = [
                {
                    'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
                },
                {
                    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
                },
                {
                    'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
                },
                {
                    'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
                },
                {   'NAME': 'registration.validators.CustomPasswordValidator',
                
                },]
                

                这篇关于如何在 django auth 密码验证器旁边使用自定义密码验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                    <bdo id='iUURi'></bdo><ul id='iUURi'></ul>
                      <tbody id='iUURi'></tbody>

                    <small id='iUURi'></small><noframes id='iUURi'>

                    • <legend id='iUURi'><style id='iUURi'><dir id='iUURi'><q id='iUURi'></q></dir></style></legend>

                        <i id='iUURi'><tr id='iUURi'><dt id='iUURi'><q id='iUURi'><span id='iUURi'><b id='iUURi'><form id='iUURi'><ins id='iUURi'></ins><ul id='iUURi'></ul><sub id='iUURi'></sub></form><legend id='iUURi'></legend><bdo id='iUURi'><pre id='iUURi'><center id='iUURi'></center></pre></bdo></b><th id='iUURi'></th></span></q></dt></tr></i><div id='iUURi'><tfoot id='iUURi'></tfoot><dl id='iUURi'><fieldset id='iUURi'></fieldset></dl></div>
                      1. <tfoot id='iUURi'></tfoot>