覆盖 password_validation 条消息
override password_validation messages
我使用 UserCreationForm 创建新用户。
from django.contrib.auth.forms import UserCreationForm
class RegistrationForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'is_active']
UserCreationForm
自动添加两个字段(Password1 和 Password2)。
如果密码太短,则会引发错误,告知该错误。或者它是否太简单或太常见。这是通过 django.contrib.auth.password_validation
.
完成的
我想知道我是否可以覆盖这些错误的消息。
现在密码验证的源代码是:
def validate(self, password, user=None):
if len(password) < self.min_length:
raise ValidationError(
ungettext(
"This password is too short. It must contain at least %(min_length)d character.",
"This password is too short. It must contain at least %(min_length)d characters.",
self.min_length
),
code='password_too_short',
params={'min_length': self.min_length},
)
但是当我尝试在表单定义中使用此代码来覆盖此错误消息时,标签发生了变化,但 error_messages 保持不变:
password1 = forms.CharField(label='New Label', error_messages={'password_too_short': 'My error message for too short passwords'})
我做错了什么?
更新:子分类而不是 copying/pasting 可能是更好的解决方案。参见 。
有关类似说明,请参阅 。
我一直在寻找同样的东西,但我认为没有任何方法可以从表单级别更改密码验证器上的错误消息。您可能最终不得不编写自己的自定义验证器,然后将其包含在 settings.py
中的 AUTH_PASSWORD_VALIDATORS
中(我就是这样做的)。这是我的做法:
1. 转到 django 的内置密码验证器并复制 MinimumLengthValidator 代码。这是 link:https://docs.djangoproject.com/en/2.0/_modules/django/contrib/auth/password_validation/#MinimumLengthValidator
2. 在您选择的应用程序中创建一个 python 文件(我选择了我的基本应用程序)并为其指定您选择的名称(我的是 custom_validators.py
)
我的看起来像这样:
from django.utils.translation import ngettext # https://docs.python.org/2/library/gettext.html#gettext.ngettext
from django.core.exceptions import ValidationError
# https://docs.djangoproject.com/en/2.0/_modules/django/contrib/auth/password_validation/#MinimumLengthValidator
class MyCustomMinimumLengthValidator(object):
def __init__(self, min_length=8): # put default min_length here
self.min_length = min_length
def validate(self, password, user=None):
if len(password) < self.min_length:
raise ValidationError(
ngettext(
# silly, I know, but if your min length is one, put your message here
"This password is too short. It must contain at least %(min_length)d character.",
# if it's more than one (which it probably is) put your message here
"This password is too short. It must contain at least %(min_length)d characters.",
self.min_length
),
code='password_too_short',
params={'min_length': self.min_length},
)
def get_help_text(self):
return ngettext(
# you can also change the help text to whatever you want for use in the templates (password.help_text)
"Your password must contain at least %(min_length)d character.",
"Your password must contain at least %(min_length)d characters.",
self.min_length
) % {'min_length': self.min_length}
3. 在 settings.py
中,注释掉 AUTH_PASSWORD_VALIDATORS
中的以下行:
# {
# 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
# },
并添加以下行:
{
'NAME': 'base.custom_validators.MyCustomMinimumLengthValidator',
# app_name.file_name.validator_name
},
现在,每当您 运行 validate()
或 form.is_valid()
(也 运行s validate()
)时,您的密码将通过新的自定义密码验证程序而不是 django 的默认值。这可能需要一些调整,但您可能会为所有 Django 的默认验证器执行此操作。
希望对您有所帮助!
你也可以使用 Django 的内置 i18n:
1. 创建文件 <PROJECT ROOT>/locale/en/LC_MESSAGES/django.po
msgid "This password is entirely numeric."
msgstr "The password should contain both letters and numbers."
2. 在settings.py
中添加
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'en-us'
3.编译消息得到django.mo
文件
django-admin compilemessages
之后,将显示消息 "The password should contain both letters and numbers." 而不是 "This password is entirely numeric."。
这样,您就可以更改所有标准消息了。
我找到了更好的解决方案:
不需要替换class的所有代码,继承classMinimumLengthValidator,修改需要的定义即可。在这种情况下验证和 get_help_text.
from django.contrib.auth.password_validation import MinimumLengthValidator
from django.core.exceptions import ValidationError
from django.utils.translation import ngettext
# https://docs.djangoproject.com/en/2.0/_modules/django/contrib/auth/password_validation/#MinimumLengthValidator
class MinimumLengthValidator(MinimumLengthValidator):
# update this definition with your custom messages
def validate(self, password, user=None):
if len(password) < self.min_length:
raise ValidationError(
ngettext(
"This password is too short. It must contain at least %(min_length)d character.",
"This password is too short. It must contain at least %(min_length)d characters.",
self.min_length
),
),
code='password_too_short',
params={'min_length': self.min_length},
)
# update this definition with your custom messages
def get_help_text(self):
return ngettext(
"Your password must contain at least %(min_length)d character.",
"Your password must contain at least %(min_length)d characters.",
self.min_length
) % {'min_length': self.min_length}
我希望这能帮助其他人解决这个问题。
对于那些只想更改验证消息的人,我找到了一个更好的解决方案,只需继承 指定的 class 并将密码验证设置变量替换为您自己的自定义 class按指定编辑
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'apps.user.validators.MinimumLengthValidatorCustom',
# 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'apps.user.validators.NumericPasswordValidatorCustom',
# 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
我使用 UserCreationForm 创建新用户。
from django.contrib.auth.forms import UserCreationForm
class RegistrationForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'is_active']
UserCreationForm
自动添加两个字段(Password1 和 Password2)。
如果密码太短,则会引发错误,告知该错误。或者它是否太简单或太常见。这是通过 django.contrib.auth.password_validation
.
我想知道我是否可以覆盖这些错误的消息。
现在密码验证的源代码是:
def validate(self, password, user=None):
if len(password) < self.min_length:
raise ValidationError(
ungettext(
"This password is too short. It must contain at least %(min_length)d character.",
"This password is too short. It must contain at least %(min_length)d characters.",
self.min_length
),
code='password_too_short',
params={'min_length': self.min_length},
)
但是当我尝试在表单定义中使用此代码来覆盖此错误消息时,标签发生了变化,但 error_messages 保持不变:
password1 = forms.CharField(label='New Label', error_messages={'password_too_short': 'My error message for too short passwords'})
我做错了什么?
更新:子分类而不是 copying/pasting 可能是更好的解决方案。参见
有关类似说明,请参阅
我一直在寻找同样的东西,但我认为没有任何方法可以从表单级别更改密码验证器上的错误消息。您可能最终不得不编写自己的自定义验证器,然后将其包含在 settings.py
中的 AUTH_PASSWORD_VALIDATORS
中(我就是这样做的)。这是我的做法:
1. 转到 django 的内置密码验证器并复制 MinimumLengthValidator 代码。这是 link:https://docs.djangoproject.com/en/2.0/_modules/django/contrib/auth/password_validation/#MinimumLengthValidator
2. 在您选择的应用程序中创建一个 python 文件(我选择了我的基本应用程序)并为其指定您选择的名称(我的是 custom_validators.py
)
我的看起来像这样:
from django.utils.translation import ngettext # https://docs.python.org/2/library/gettext.html#gettext.ngettext
from django.core.exceptions import ValidationError
# https://docs.djangoproject.com/en/2.0/_modules/django/contrib/auth/password_validation/#MinimumLengthValidator
class MyCustomMinimumLengthValidator(object):
def __init__(self, min_length=8): # put default min_length here
self.min_length = min_length
def validate(self, password, user=None):
if len(password) < self.min_length:
raise ValidationError(
ngettext(
# silly, I know, but if your min length is one, put your message here
"This password is too short. It must contain at least %(min_length)d character.",
# if it's more than one (which it probably is) put your message here
"This password is too short. It must contain at least %(min_length)d characters.",
self.min_length
),
code='password_too_short',
params={'min_length': self.min_length},
)
def get_help_text(self):
return ngettext(
# you can also change the help text to whatever you want for use in the templates (password.help_text)
"Your password must contain at least %(min_length)d character.",
"Your password must contain at least %(min_length)d characters.",
self.min_length
) % {'min_length': self.min_length}
3. 在 settings.py
中,注释掉 AUTH_PASSWORD_VALIDATORS
中的以下行:
# {
# 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
# },
并添加以下行:
{
'NAME': 'base.custom_validators.MyCustomMinimumLengthValidator',
# app_name.file_name.validator_name
},
现在,每当您 运行 validate()
或 form.is_valid()
(也 运行s validate()
)时,您的密码将通过新的自定义密码验证程序而不是 django 的默认值。这可能需要一些调整,但您可能会为所有 Django 的默认验证器执行此操作。
希望对您有所帮助!
你也可以使用 Django 的内置 i18n:
1. 创建文件 <PROJECT ROOT>/locale/en/LC_MESSAGES/django.po
msgid "This password is entirely numeric."
msgstr "The password should contain both letters and numbers."
2. 在settings.py
中添加
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'en-us'
3.编译消息得到django.mo
文件
django-admin compilemessages
之后,将显示消息 "The password should contain both letters and numbers." 而不是 "This password is entirely numeric."。
这样,您就可以更改所有标准消息了。
我找到了更好的解决方案:
不需要替换class的所有代码,继承classMinimumLengthValidator,修改需要的定义即可。在这种情况下验证和 get_help_text.
from django.contrib.auth.password_validation import MinimumLengthValidator
from django.core.exceptions import ValidationError
from django.utils.translation import ngettext
# https://docs.djangoproject.com/en/2.0/_modules/django/contrib/auth/password_validation/#MinimumLengthValidator
class MinimumLengthValidator(MinimumLengthValidator):
# update this definition with your custom messages
def validate(self, password, user=None):
if len(password) < self.min_length:
raise ValidationError(
ngettext(
"This password is too short. It must contain at least %(min_length)d character.",
"This password is too short. It must contain at least %(min_length)d characters.",
self.min_length
),
),
code='password_too_short',
params={'min_length': self.min_length},
)
# update this definition with your custom messages
def get_help_text(self):
return ngettext(
"Your password must contain at least %(min_length)d character.",
"Your password must contain at least %(min_length)d characters.",
self.min_length
) % {'min_length': self.min_length}
我希望这能帮助其他人解决这个问题。
对于那些只想更改验证消息的人,我找到了一个更好的解决方案,只需继承
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'apps.user.validators.MinimumLengthValidatorCustom',
# 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'apps.user.validators.NumericPasswordValidatorCustom',
# 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]