尝试在 Google App Engine 中呈现 Django 模板文件时
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))
我遇到了以下错误:
<type 'exceptions.UnicodeDecodeError'>: 'ascii' codec can't decode byte 0xe2 in position 17692: ordinal not in range(128)
args = ('ascii', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Str...07/a-beautiful-method-to-find-peace-of-mind/ -->
', 17692, 17693, 'ordinal not in range(128)')
encoding = 'ascii'
end = 17693
message = ''
object = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Str...07/a-beautiful-method-to-find-peace-of-mind/ -->
reason = 'ordinal not in range(128)'
start = 17692
似乎底层的django 模板引擎采用了“ascii”编码,应该是“utf-8”。 任何人都知道可能导致麻烦的原因以及如何解决它? 谢谢。
最佳答案
原来模板返回的渲染结果需要先解码:
self.response.out.write (template.render( templatepath , template_values).decode('utf-8') )
一个愚蠢的错误,但还是感谢大家的回答。 :)
关于python - Google App Engine模板unicode解码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1139151/