我现在正在学习一本书“使用Google App Engine by Charles Severance”。Python,Google App Engine错误:断言类型(数据)是StringType,“write()参数必须是字符串”
我在第6章,到目前为止我已在模板文件夹中创建app.yaml
,index.py
,index.html
,并在静态文件夹中创建了CSS文件。
我index.py
看起来像这样:
import os
import wsgiref.appengine.ext import webapp
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class MainHandler(webapp.RequestHandler):
def get(self):
path=self.request.path
temp=os.path.join(os.path.dirname(__file__, 'templates' + path)
if not os.path.isfile(temp):
temp=os.path.join(os.path.dirname(__file__, 'templates/index.html')
self.response.out.write(template.render(temp,{}))
def main():
application = webapp.WSGIApplication([('/.*', MainHandler)], debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name == '__main__':
main()
为什么我得到这个错误?
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
self.write(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/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
我试过unicode,但没有成功。然后我试着把str作为一个字符串来输出,然后它就起作用了!谢谢您的回复! – Jules
为我完美工作! – rogcg