3

I have the following files

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)

hello world
哈罗世界
${VARIABLE}

I had refereed to the instruction from 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)

Anything I had missed out?

4

2 回答 2

6
template = Template(filename='../template/dummy.html', default_filters=['decode.utf8'], input_encoding='utf-8', output_encoding='utf-8')
于 2010-12-22T05:25:11.527 回答
2

是的,因为您试图将其渲染为 ASCII,但这是行不通的。您需要说明要使用的 output_encoding :

Template(filename='../template/dummy.html', output_encoding='utf8')

并且请不要有任何例外。添加您希望捕获的异常。

于 2010-12-21T09:07:04.957 回答