django __unicode__() - how can i call this method in a template

﹥>﹥吖頭↗ 提交于 2019-12-10 13:09:59

问题


I defined a unicode() method in my Contact model.

def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

Now I want to show the return value of the unicode() method in a template.

But all that i try fails.

{{ object.unicode }}

or

{{ object.unicode() }}

or

{{ object.__unicode__ }}

or

{{ object.str }}

That confuses me since I have another Model level function which can be referenced to from the template without problems.

This works fine:

def get_id(self):
        return "%i" % self.id 

{{ object.get_id|escape }}

回答1:


{{ object }}

Will automatically return the value of __unicode__ for any object.



来源:https://stackoverflow.com/questions/1980862/django-unicode-how-can-i-call-this-method-in-a-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!