python - Django表单中标签值之前的下划线是什么意思

标签 python django django-forms

我通过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/

相关文章:

python - 如何在 Python 中的父进程和 fork 子进程之间共享数据?

Django 模型 - 选择与外键?

django - Django 1.7 在 mod-wsgi 上的代码更改监控出现故障

python - Django 评论垃圾邮件过滤器

django - 如何使用 Django ModelForm 生成下拉输入?

python - 非均匀间距的 Numpy 或 SciPy 导数函数?

Python删除firefox历史记录

python - numpy.recarray.tobytes 的逆

python - uWSGI-导入错误: No module named os

django - 使用 CreateView 将请求变量传递给模型的最佳方法是什么