开发者

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

开发者 https://www.devze.com 2022-12-15 16:02 出处:网络
I defined a unicode() method in my Contact model. def __unicode__(self): return u\'%s %s\' % (self.first_name, self.last_name)

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 unic开发者_开发知识库ode() 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 }}


{{ object }}

Will automatically return the value of __unicode__ for any object.

0

精彩评论

暂无评论...
验证码 换一张
取 消