我通过django源代码中的UserCreationForm
来了here 。
这是问题,如果您查看两个密码字段,您会在标签值之前看到一个下划线,我想知道它的用途是什么,因为自从我搜索它以来,它没有写在文档中。
class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'password_mismatch': _('The two password fields didn’t match.'),
}
password1 = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
strip=False,
help_text=_("Enter the same password as before, for verification."),
)
最佳答案
这是 gettext_lazy(…)
function [Django-doc] 的别名。事实上,在 line 19 [GitHub] ,我们看到:
from django.utils.translation import gettext, <b>gettext_lazy as _</b>
这将延迟翻译消息。这意味着如果您在模板中渲染它,模板将在惰性翻译对象上调用 str(…)
。这将根据激活的语言提供标签的翻译版本。
有关翻译的更多信息,请参阅Tranlation section of the Django documentation .
关于python - Django表单中标签值之前的下划线是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63550170/