python – Mako模板中的UnicodeEncodeError

我有以下文件

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(以UTF-8格式保存)

hello world
哈罗世界
${VARIABLE}

我已经审阅了http://www.makotemplates.org/docs/unicode.html的指令

但是,我仍然得到错误

UnicodeDecodeError: ‘ascii’ codec
can’t decode byte 0xe6 in position 0:
ordinal not in range(128)

我错过了什么?

最佳答案

template = Template(filename='../template/dummy.html', default_filters=['decode.utf8'], input_encoding='utf-8', output_encoding='utf-8')
点赞