提问人:Barsha Nayak 提问时间:9/11/2023 更新时间:11/18/2023 访问量:56
无法导入验证程序 检查您的AUTH_PASSWORD_VALIDATORS设置
validator could not be imported Check your AUTH_PASSWORD_VALIDATORS setting
问:
我制作了 validators.py 并添加了我的自定义密码验证器,例如检查大写和特殊字符,然后我在 settings.py 的身份验证密码验证器中添加了这些验证器,如下所示
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
'OPTIONS': {
'max_similarity': 0.7,
'user_attributes': ("username", "first_name", "last_name", "email")
}
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 8,
}
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
\# custom coded validators
{
'NAME': 'app1.validators.UppercaseValidator',
},
{
'NAME': 'app1.validators.SpecialCharValidator',
},
]
这是我 forms.py
`
from django import forms from django.contrib.auth.password_validation import validate_password class SignupForm(forms.Form):
firstname = forms.CharField(max_length=20)
lastname = forms.CharField(max_length=20)
email = forms.EmailField()
username = forms.CharField(max_length=30)
password = forms.CharField(widget=forms.PasswordInput, validators=[validate_password])`
在注册时,我收到此错误
ImproperlyConfigured at /signup/ The module in NAME could not be imported: app1.validators.UppercaseValidator. Check your AUTH_PASSWORD_VALIDATORS setting.
任何帮助都是值得赞赏的
在它基于Django默认具有的验证器给我错误之前,但是在添加我自己的验证器之后,它给了我我提到的错误
答:
1赞
alessandro dellatorre
10/9/2023
#1
我在运行我的 django 实例时遇到了同样的问题 它与版本(3、2、19、'final'、0)一起工作正常,而与版本(4、1、7、'final'、0)一起崩溃。 这是我的大写代码:
import re
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
class UppercaseValidator:
def validate(self, password, user=None):
if not re.findall('[A-Z]', password):
raise ValidationError(
_("The password must contain at least 1 uppercase letter, A-Z."),
code='password_no_upper',
)
def get_help_text(self):
return _(
"Your password must contain at least 1 uppercase letter, A-Z."
)
这是我 settings.py:
AUTH_PASSWORD_VALIDATORS = [
# custom coded validators
{'NAME': 'users.validators.UppercaseValidator',},
...
]
因此,我将验证移到了 serializer.py 中,用 settings.py 注释了代码,它起作用了。 这是我 serializer.py 的代码:
import re
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
class CustomRegisterSerializer(RegisterSerializer):
def validate(self, data):
user = User(**data)
password = data.get('password')
try:
if not re.findall('[A-Z]', password):
raise serializers.ValidationError("The password must contain at least 1 uppercase letter, A-Z.")
except Exception as e:
raise serializers.ValidationError("The password must contain at least 1 uppercase letter, A-Z.")
希望这对你有所帮助!
0赞
Richard Haimann
11/11/2023
#2
改变
from django.utils.translation import ugettext as _
自
from django.utils.translation import gettext as _
ugettext
在 Django 3.0 中已被弃用并替换为。gettext
评论
1赞
OCa
11/15/2023
参考 doc 会有所帮助吗?
评论