1
我在Google App Engine上使用webapp2框架,并在我的一个请求处理程序中收到基本错误。使用Webapp2部署的GAE RequestHandler中的错误
应用程序是运行在本地实例确定,但会导致对谷歌的App Engine部署版本如下回溯:
下面的代码:
import os
from google.appengine.ext.webapp import template
import webapp2
import logging
class MainHandler(webapp2.RequestHandler):
def get(self):
logging.info('hi there 34')
template_values = {}
self.response.out.write('hello world 4')
path = os.path.join(os.path.dirname(__file__), 'index.html')
## This is the code that causes the bug ##
self.response.out.write(template.render(path, template_values))
## ## ## ##
debug = os.environ.get('SERVER_SOFTWARE', '').startswith('Dev')
app = webapp2.WSGIApplication(
[(r'/main', MainHandler)],
debug = debug)
def main():
app.run()
追踪误差:
Traceback (most recent call last):
File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py",
line 86, in run
self.finish_response()
File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py",
line 127, in finish_response
self.write(data)
File "/base/python27_runtime/python27_dist/lib/python2.7/wsgiref/handlers.py",
line 202, in write
assert type(data) is StringType,"write() argument must be string"
AssertionError: write() argument must be string
这个错误是什么意思?
感谢您的回答。通常使用GAE,我从来不需要编码到utf-8中。但是,您的答案确实让我想起了为响应呈现的html文件。我的代码编辑器已经自动插入html标记,使html转换为utf-8。这是我第一次将这个标记加入到html文件中,也是我第一次看到这个特定的错误。但是,唉,同样的错误重复自己与该标记或不。并按照你的建议编码,没有它。然而,我赞成你的回答,因为我认为这是一个好的预感,可以让其他人受益。任何其他想法? – 2011-12-19 17:44:42
我刚刚通过将渲染的模板值转换为字符串来重试此操作,并且它工作正常。不知道是什么原因,因为这是我第一次这样做。通常在我之前的应用程序中,以及GAE文档中的所有示例中,template.render(路径,值)按原样运行。再次感谢您指向我的文本编码方向! – 2011-12-19 17:53:42
Jinja2不支持utf-8字节串。 'Template.render'总是返回unicode字符串。如果您将非Unicode字符的非Unicode字符传递给JINJA模板,您将得到Unicode错误。 – Ski 2011-12-19 20:32:53