4

I'm trying to do a very simple thing but I couldn't find how to.

I have this function defined in my model class :

def __unicode__(self):
    return "Object name = " + self.name

so in a template page, or in a views.py, I want to get this string.

in a template, I tried these and they didn't work :

{{ my_object }} {{ str(my_object) }} {{ my_object.self }}

What is the correct way of doing this?

Thanks !

1

1 Answer 1

8

There is two functions __unicode__ and __str__ for python class.

str function, even in template, calls __str__.

class Object(object):
    name = "I'm an object"

    def __str__(self):
        return "Object name = %s" % self.name

    def __unicode__(self):
        return u"Object name = %s" % self.name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.