我正在尝试为 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_lazy as _
关于python - Django 表单自定义模型表单字段标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34953695/