Python – Google App engine template unicode decoding problem


When trying to render a Django template file in Google App Engine

from google.appengine.ext.webapp import template

templatepath = os.path.join(os.path.dirname(file), 'template.html')
self.response.out.write (template.render( templatepath , template_values))

I come across the following error:

'ascii' codec can't decode byte 0xe2
in position 17692: ordinal not in
args = ('ascii', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
', 17692, 17693, 'ordinal not in range(128)')
encoding = 'ascii'
end = 17693
message = ''
object = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
reason = 'ordinal not in range(128)'
start = 17692

It seems that the underlying django template engine has assumed the "ascii" encoding, which should have been "utf-8".
Anyone who knows what might have caused the trouble and how to solve it?

Best Solution

Well, turns out the rendered results returned by the template needs to be decoded first:

self.response.out.write (template.render( templatepath , template_values).decode('utf-8') )

A silly mistake, but thanks for everyone's answers anyway. :)