[英]UnicodeEncodeError in Mako Template
I have the following files 我有以下文件
dummy.py dummy.py
#!c:/Python27/python.exe -u
from mako import exceptions
from mako.template import Template
print "Content-type: text/html"
print
#VARIABLE = "WE"
VARIABLE = "我们"
template = Template(filename='../template/dummy.html', output_encoding='utf8')
try:
print template.render(VARIABLE=VARIABLE)
except:
print exceptions.html_error_template().render()
dummy.html (Saved in UTF-8 format) dummy.html (以UTF-8格式保存)
hello world
哈罗世界
${VARIABLE}
I had refereed to the instruction from http://www.makotemplates.org/docs/unicode.html 我已经参考了http://www.makotemplates.org/docs/unicode.html的指示
However, I still get error 但是,我仍然得到错误
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
UnicodeDecodeError:'ascii'编解码器无法解码位置0中的字节0xe6:序数不在范围内(128)
Anything I had missed out? 我错过了什么?
template = Template(filename='../template/dummy.html', default_filters=['decode.utf8'], input_encoding='utf-8', output_encoding='utf-8')
Yes, because you are trying to render it to ASCII, which doesn't work. 是的,因为您试图将其呈现为ASCII,这不起作用。 You need to say what output_encoding to use:
您需要说出要使用的output_encoding:
Template(filename='../template/dummy.html', output_encoding='utf8')
And please don't have bare excepts. 请没有裸露的例外。 Add what exceptions you expect to catch.
添加您期望捕获的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.