I’m trying to put a degree sign into a string that gets passed to a template. I’ve tried ° which of course got escaped. Then I tried using \u00b0 and \xb0 like so:
def __str__(self):
return u"%s (%s \xb0C to %s \xb0C)" % \
(self.name, self.min_deg_c, self.max_deg_c)
Both of these have resulted in:
'ascii' codec can't encode character u'\xb0' in position 14: ordinal not in range(128)
Ideally django would just turn \xb0 into ° when escaping. What can I do to get this result? I don’t want to turn off escaping.
You can’t return unicode from a
__str__method (assuming you’re using Python 2.x). You should use the__unicode__method.