Django错误:当我尝试创建新模型时,未定义名称“ _”

时间:2018-11-20 11:10:45

标签: python

我定义了此模型

from django.db import models
from django.db.models import CharField
# Create your models here.
class City(models.Model):
    state = models.ForeignKey('state.State',on_delete=models.CASCADE,)
    city_name = models.CharField(max_length=100, verbose_name=_('City Name'))

    def __unicode__(self, ):
        return str(self.city_name)

但是当我要使用它时,出现此错误 在城市

city_name = models.CharField(max_length=100, verbose_name=_('City Name'))
NameError: name '_' is not defined

我不知道问题出在哪里

1 个答案:

答案 0 :(得分:2)

您需要导入gettex或替代函数from django.utils.translation import gettext as _。看看django i18n documentation