简体   繁体   中英

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". I've got a couple problems working correctly until I modified the main python file when I started getting blank browser page.

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

I'm not sure what I'm missing but here's the python code.

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.

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). In your case, you have wrapped this in a main() function, which explains why it is not working anymore.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM