2013-12-13 43 views
1

我一直在嘗試使用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

什麼是你的index.html的內容? – brian

回答

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) 
+0

Thankx Brian。我知道我應該使用webapp2和Jinja,但是當我剛剛進入它時,我認爲如果我在本書中使用示例,這是有意義的。我試過你建議的代碼,不幸的是,它仍然無法正常工作。 – Afloz

+0

是的,我確定我明白你想繼續與你的書,這只是要記住,現在我們使用webapp2。無論如何,奇怪的是代碼不起作用,它在我的開發環境中適用於我。你能告訴我你的工作文件夾的結構嗎? – brian

+0

Thankx Brian。我已經離開了這本書。我讀過評論,它充滿了諸如此類的錯誤。謝謝您的幫助。 – Afloz

相關問題