Django 表单自定义模型表单字段标签
Django forms Customising model form field labels
我正在尝试为 Django 模型表单自定义标签:
class SupplyTypeForm(forms.ModelForm):
class Meta:
model = EUser
fields = ('service_type', 'online_account')
labels = {
'online_account': _('Do you have an online account with any of your suppliers'),
}
但我收到错误:NameError: name '_' is not defined
但是 django 文档提到这样做,所以我不清楚哪里出了问题(下划线很奇怪,我不确定为什么在这里使用它)。如果我删除它,错误就会消失
文档有它的任何原因:https://docs.djangoproject.com/en/stable/topics/forms/modelforms/#overriding-the-default-fields
您应该添加 from django.utils.translation import ugettext as _
以使用 _()
您需要确保导入正确:
from django.utils.translation import ugettext_lazy as _
我正在尝试为 Django 模型表单自定义标签:
class SupplyTypeForm(forms.ModelForm):
class Meta:
model = EUser
fields = ('service_type', 'online_account')
labels = {
'online_account': _('Do you have an online account with any of your suppliers'),
}
但我收到错误:NameError: name '_' is not defined
但是 django 文档提到这样做,所以我不清楚哪里出了问题(下划线很奇怪,我不确定为什么在这里使用它)。如果我删除它,错误就会消失
文档有它的任何原因:https://docs.djangoproject.com/en/stable/topics/forms/modelforms/#overriding-the-default-fields
您应该添加 from django.utils.translation import ugettext as _
以使用 _()
您需要确保导入正确:
from django.utils.translation import ugettext_lazy as _