【问题标题】:Google App Engine - Blank Page on Browser but No ErrorGoogle App Engine - 浏览器上的空白页面但没有错误
【发布时间】:2013-12-13 12:29:31
【问题描述】:

我一直在尝试 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()

如您所见,控制台没有报告错误,但在浏览器中查看时,空白屏幕盯着我。我错过了什么吗?

【问题讨论】:

  • 你的 index.html 的内容是什么?

标签: python google-app-engine web-applications


【解决方案1】:

首先我认为你的教程已经过时了,因为它仍然使用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)

【讨论】:

  • 谢谢布赖恩。我知道我应该使用 webapp2 和 Jinja,但是当我刚刚进入它时,我认为如果我使用书中的示例是有意义的。我尝试了您建议的代码,不幸的是,它仍然无法正常工作。
  • 是的,我明白你想继续写你的书,这只是要记住的一点,现在我们使用 webapp2。无论如何,奇怪的是代码不起作用,它在我的开发环境中对我有用。你能告诉我你的工作文件夹的结构吗?
  • 谢谢布赖恩。我已经离开了书。我读过评论说它充满了诸如此类的错误。感谢您的帮助。
猜你喜欢
  • 2014-06-26
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 2011-08-21
  • 1970-01-01
  • 2021-11-19
相关资源
最近更新 更多