When trying to render a Django template file in Google App Engine
尝试在Google App Engine中呈现Django模板文件时
from google.appengine.ext.webapp import template
来自google.appengine.ext.webapp导入模板
templatepath = os.path.join(os.path.dirname(file), 'template.html')
self.response.out.write (template.render( templatepath , template_values))templatepath = os.path.join(os.path.dirname(file),'template.html')self.response.out.write(template.render(templatepath,template_values))
I come across the following error:
我遇到以下错误:
<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
:'ascii'编解码器无法解码17692位的字节0xe2:序号不在范围内(128)args =('ascii','',17692,17693,'ordinal not in range(128)')encoding ='ascii'end = 17693 message =''object =' reason ='序数不在范围内(128)'start = 17692 'exception.unicodedecodeerror'>
It seems that the underlying django template engine has assumed the "ascii" encoding, which should have been "utf-8". Anyone who knows what might have caused the trouble and how to solve it? Thanks.
似乎底层的django模板引擎已经采用了“ascii”编码,它应该是“utf-8”。谁知道什么可能导致麻烦以及如何解决?谢谢。
3 个解决方案
#1
Well, turns out the rendered results returned by the template needs to be decoded first:
好吧,结果是模板返回的渲染结果需要先解码:
self.response.out.write (template.render( templatepath , template_values).decode('utf-8') )
self.response.out.write(template.render(templatepath,template_values).decode('utf-8'))
A silly mistake, but thanks for everyone's answers anyway. :)
一个愚蠢的错误,但无论如何,谢谢大家的答案。 :)
#2
Are you using Django 0.96 or Django 1.0? You can check by looking at your main.py and seeing if it contains the following:
你在使用Django 0.96还是Django 1.0?您可以通过查看main.py进行检查,看看它是否包含以下内容:
from google.appengine.dist import use_library use_library('django', '1.0')
If you're using Django 1.0, both FILE_CHARSET and DEFAULT_CHARSET should default to 'utf-8'. If your template is saved under a different encoding, just set FILE_CHARSET to whatever that is.
如果您使用的是Django 1.0,则FILE_CHARSET和DEFAULT_CHARSET都应默认为'utf-8'。如果您的模板以不同的编码保存,只需将FILE_CHARSET设置为其他任何编码即可。
If you're using Django 0.96, you might want to try directly reading the template from the disk and then manually handling the encoding.
如果你使用的是Django 0.96,你可能想尝试直接从磁盘读取模板,然后手动处理编码。
e.g., replace
template.render( templatepath , template_values)
template.render(templatepath,template_values)
with
Template(unicode(template_fh.read(), 'utf-8')).render(template_values)
#3
Did you check in your text editor that the template is encoded in utf-8?
您是否在文本编辑器中检查了模板是否以utf-8编码?
#1
Well, turns out the rendered results returned by the template needs to be decoded first:
好吧,结果是模板返回的渲染结果需要先解码:
self.response.out.write (template.render( templatepath , template_values).decode('utf-8') )
self.response.out.write(template.render(templatepath,template_values).decode('utf-8'))
A silly mistake, but thanks for everyone's answers anyway. :)
一个愚蠢的错误,但无论如何,谢谢大家的答案。 :)
#2
Are you using Django 0.96 or Django 1.0? You can check by looking at your main.py and seeing if it contains the following:
你在使用Django 0.96还是Django 1.0?您可以通过查看main.py进行检查,看看它是否包含以下内容:
from google.appengine.dist import use_library use_library('django', '1.0')
If you're using Django 1.0, both FILE_CHARSET and DEFAULT_CHARSET should default to 'utf-8'. If your template is saved under a different encoding, just set FILE_CHARSET to whatever that is.
如果您使用的是Django 1.0,则FILE_CHARSET和DEFAULT_CHARSET都应默认为'utf-8'。如果您的模板以不同的编码保存,只需将FILE_CHARSET设置为其他任何编码即可。
If you're using Django 0.96, you might want to try directly reading the template from the disk and then manually handling the encoding.
如果你使用的是Django 0.96,你可能想尝试直接从磁盘读取模板,然后手动处理编码。
e.g., replace
template.render( templatepath , template_values)
template.render(templatepath,template_values)
with
Template(unicode(template_fh.read(), 'utf-8')).render(template_values)
#3
Did you check in your text editor that the template is encoded in utf-8?
您是否在文本编辑器中检查了模板是否以utf-8编码?