python - Google App Engine模板unicode解码问题

标签 python django google-app-engine unicode

尝试在 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/

相关文章:

python - ElasticSearch:查找具有数组中字段值的文档

google-app-engine - 如何在 GQL 中选择没有引用另一个模型类的所有行?

google-app-engine - 任务队列 API : ETA and Countdown

python - 为除一个元素外的整个图像添加透明度

python - 遍历数组查看非连续值

python - Django 中按降序排列类别

python - 如何在Django中使用CreateView进行多个文件的上传?

java - 如何在 Google App Engine 中提取 rar 文件 (AppEngineFile)

python - 动画+在矩阵之间平滑插值

python - 在 Django 中跟踪主干安全吗?