[英]Google App Engine - Blank Page on Browser but No Error
I've been trying out on GAE/Python taking examples from the book "Using Google App Engine". 我一直在尝试GAE / Python,并从“使用Google App Engine”一书中举例说明。 I've got a couple problems working correctly until I modified the main python file when I started getting blank browser page.
在开始获取空白的浏览器页面之前,我修改了主要的python文件之前,我无法正常工作。
Log Console 日志控制台
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 index.py
I'm not sure what I'm missing but here's the python code. 我不确定我缺少什么,但这是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()
As you can observe, the console reports no error but when viewed in the browser, a blank screen stares back at me. 如您所见,控制台未报告任何错误,但是在浏览器中查看时,空白屏幕向我回望。 Am I missing something?
我想念什么吗?
First of all I think you tutorial is outdated, as it still uses webapp
and you should probably use webapp2
now. 首先,我认为您的教程已经过时了,因为它仍然使用
webapp
,因此您现在应该使用webapp2
。
That being said, your code is handled as an App Engine module, therefore it is looking for a property named app
(in older versions it was application
as stated in your code). 就是说,您的代码被作为App Engine模块处理,因此它正在寻找一个名为
app
的属性(在旧版本中,它是您的代码中所述的application
)。 In your case, you have wrapped this in a main() function, which explains why it is not working anymore. 对于您而言,已将其包装在main()函数中,这解释了为什么它不再起作用了。
Just make your code look like this and it should be ok: 只要使您的代码看起来像这样,就可以了:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.