我一直在试用GAE / Python,从“使用Google App Engine”一书中获取示例。我有一些问题正常工作,直到我开始获取空白浏览器页面时修改主python文件。
日志控制台
INFO 2013-12-13 21:17:34,568 module.py:617] default: "GET / HTTP/1.1" 200 -
INFO 2013-12-13 21:17:34,737 module.py:617] default: "GET /favicon.ico HTTP/1.1" 200 -
index.py
我不确定我缺少什么,但这是python代码。
import os
import logging
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
def doRender(handler, tname='index.html', values={}):
#Check if the template file tname exists
temp = os.path.join(os.path.dirname(__file__),
'template/' + tname)
if not os.path.isfile(temp):
return False
# If template file exisis, make a copy and add the path
newval = dict(values)
newval['path'] = handler.request.path
outstr = template.render(temp, newval)
handler.response.out.write(str(outstr))
return True
class LoginHandler(webapp.RequestHandler):
def get(self):
doRender(self, 'loginscreen.html')
def post(self):
acct = self.request.get('account')
pw = self.request.get('password')
logging.info('Checking account = '+acct+' pw='+pw)
if pw == '' or acct == '':
doRender(self, 'loginscreen.html', {'error': 'Please specify Account and Password'} )
elif pw == 'secret':
doRender(self, 'loggedin.html', {})
else:
doRender(self, 'loginscreen.html', {'error': 'Incorrect password'} )
class MainHandler(webapp.RequestHandler):
def get(self):
if doRender(self, self.request.path):
return
doRender(self, 'index.html')
def main():
application = webapp.WSGIApplication([
('/login', LoginHandler),
('/.*', MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
正如您所看到的,控制台报告没有错误,但在浏览器中查看时,一个空白屏幕会回头看我。我错过了什么吗?
答案 0 :(得分:0)
首先,我认为您的教程已过时,因为它仍然使用webapp
,您现在应该使用webapp2
。
话虽如此,您的代码将作为App Engine模块处理,因此它正在寻找名为app
的属性(在旧版本中,它是application
,如您的代码中所述)。在你的情况下,你已经将它包装在main()函数中,这解释了为什么它不再起作用了。
只需让你的代码看起来像这样就可以了:
import os
import logging
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
def doRender(handler, tname='index.html', values={}):
#Check if the template file tname exists
temp = os.path.join(os.path.dirname(__file__),
'template/' + tname)
if not os.path.isfile(temp):
return False
# If template file exisis, make a copy and add the path
newval = dict(values)
newval['path'] = handler.request.path
outstr = template.render(temp, newval)
handler.response.out.write(str(outstr))
return True
class LoginHandler(webapp.RequestHandler):
def get(self):
doRender(self, 'loginscreen.html')
def post(self):
acct = self.request.get('account')
pw = self.request.get('password')
logging.info('Checking account = '+acct+' pw='+pw)
if pw == '' or acct == '':
doRender(self, 'loginscreen.html', {'error': 'Please specify Account and Password'} )
elif pw == 'secret':
doRender(self, 'loggedin.html', {})
else:
doRender(self, 'loginscreen.html', {'error': 'Incorrect password'} )
class MainHandler(webapp.RequestHandler):
def get(self):
if doRender(self, self.request.path):
return
doRender(self, 'index.html')
app = webapp.WSGIApplication([
('/login', LoginHandler),
('/.*', MainHandler)],
debug=True)