Django表单自定义模型表单字段标签

Yun*_*nti 2 python django django-forms

我正在尝试为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'),
        }
Run Code Online (Sandbox Code Playgroud)

但我得到错误:NameError:名称'_'未定义

然而django文档提到这样做,所以我不清楚什么是错的(下划线很奇怪,我不确定为什么在这里使用它).如果我删除它工作,错误消失

文档拥有它的任何理由:https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/#overriding-the-default-fields

gtl*_*ert 5

您需要确保输入正确:

from django.utils.translation import ugettext_lazy as _
Run Code Online (Sandbox Code Playgroud)