[Fixed]-Python unicode #-*- coding: utf-8 -*- not doing the job…where to encode?I think I need to remove str and encode

1👍

The __unicode__() magic method MUST return a unicode string (an instance of the unicode type), but you are returning a byte string (instance of the str type).

Adding the “# coding” mark on top of you code won’t turn byte strings into unicode ones, it will only tells Python that your byte strings litterals are utf-8 encoded – but they are still byte strings.

The solution is dead simple: make sure you return a unicode string. First make sure each and every string in your context is unicode, then make all your litteral strings unicode too by prefixing them with a u, ie:

return u"%(sender)s %(verb)s <a href='%(verify_read)s?next=%(target_url)s'>%(target)s</a> with %(action)s" % context
# ...
return u"%(sender)s %(verb)s %(target)s with %(action)s" % context
# ...
return u"%(sender)s %(verb)s %(target)s" % context
# ...
return u"%(sender)s %(verb)s" %  context

If you don’t grasp the difference between a unicode string and a utf-8 encoded byte string, you definitly want to read this : http://www.joelonsoftware.com/articles/Unicode.html

Leave a comment