【发布时间】:2010-12-31 03:40:04
【问题描述】:
我在我的联系人模型中定义了一个 unicode() 方法。
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
现在我想在模板中显示 unicode() 方法的返回值。
但我尝试的一切都失败了。
{{ object.unicode }}
或
{{ object.unicode() }}
或
{{ object.__unicode__ }}
或
{{ object.str }}
这让我很困惑,因为我有另一个模型级别的函数,可以毫无问题地从模板中引用。
这很好用:
def get_id(self):
return "%i" % self.id
{{ object.get_id|escape }}
【问题讨论】:
标签: django-models django-templates