Search code examples
pythonpython-2.7mod-wsgimako

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5


UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 537: ordinal not in range(128), referer: ...

I always get this error when I try to output my whole website with characters "č". I am using mako templating. What to do?


Solution

  • The error occurs because somewhere code coerces your unicode template string into a python 2 str; you need to encode the rendered template into an UTF-8 bytestring yourself:

    if isinstance(rendered, unicode):
        rendered = rendered.encode('UTF-8')
    
    # rendered is now guaranteed to be of type str