1

我試過好老留言例如,從這個谷歌GAE教程視頻遷移: http://www.youtube.com/watch?gl=DE&hl=de&v=bfgO-LXGpTMWeb應用程序不會自動從GAE遷移蟒蛇重載2.5.2 + web應用程序到2.7 + webapp2的

問題:當我跳通過self.redirect('/')到主類,頁面不會自動重新加載。我需要手動重新加載瀏覽器窗口(例如通過F5)來查看最新的留言簿條目,這是在MakeGuestbookEntry類中完成的。

用python 2.5.2 + webapp這個問題不存在。

這是Python代碼文件main.py:

#!/usr/bin/env python 
Import OS, says 
import wsgiref.handlers 
import webapp2 
from google.appengine.ext import db 
from google.appengine.ext.webapp.util import run_wsgi_app 
from google.appengine.ext.webapp import template 

class guestbook(db.Model): 
    message = db.StringProperty(required=True) 
    when = db.DateTimeProperty(auto_now_add=True) 
    who = db.StringProperty() 

class ShowGuestbookPage(webapp2.RequestHandler): 
    def get(self): 
    # Read from the Datastore 
    shouts = db.GqlQuery('SELECT * FROM guestbook ORDER BY when DESC') 
    values = {'shouts': shouts} 
    self.response.out.write(template.render('main.html', values)) 

class MakeGuestbookEntry(webapp2.RequestHandler): 
    def post(self): 
    shout = guestbook(message=self.request.get('message'), who=self.request.get('who')) 
    # Write into the datastore 
    shout.put() 
    self.redirect('/') 

app = webapp2.WSGIApplication([('/', ShowGuestbookPage), 
           ('/make_entry', MakeGuestbookEntry), 
           debug=True) 

def main(): 
    run_wsgi_app(app) 

if __name__ == "__main__": 
    main() 

這是HTML頁面main.html中

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     "http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
<head> 
<title>Simple Guestbook with the Google App Engine</title> 
</head> 
<body> 

<h3>Simple Guestbook with the Google App Engine</h3> 

{% for shout in shouts %} 
<div> 
    {% if shout.who %} 
     <b>{{shout.who}}</b> 
    {% else %} 
     <b>Anonymous</b> 
    {% endif %} 
    sagt: 
    <b>{{shout.message}}</b> 
</div> 
{% endfor %} 

<p> 

<form action="make_entry" method="post" accept-charset="utf-8"> 
Name: <input type="text" size="20" name="who" value="" if="who"> 
Nachricht: <input type="text" size="20" name="message" value="" if="message"> 
<input type="submit" value="Absenden"> 
</form> 

</body> 
</html> 

感謝您的幫助。 最好的問候

回答

1

那個教程很老。我建議你使用Getting Started guide中最新的留言板教程。

這樣做的原因行爲,特別是如果你在開發服務器,是GAE現在模擬最終一致性。基本上,這意味着您的新添加的留言條目將不會顯示在您的應用立即運行的所有服務器上。有些用戶可能會立即看到它,有些則可能不會。確保獲得最新數據的一個好方法是刷新頁面並強制應用程序加載它......但當然,您不能期望用戶喜歡這樣的內容:P

新的留言板教程使用祖先查詢,而強制執行強一致性。換句話說,用戶將立即看到更新,無需刷新頁面!您可以閱讀更多關於強一致性here

+0

非常感謝!這導致「問題」。這不是一個真正的問題,因爲它按設計工作。但在我看來,這使得前一個簡單易用的系統變得複雜。你知道有什麼方法可以強制使用強一致性,而不必更改源代碼嗎?我試圖理解Google的解釋,但是我無法以其正常工作的方式更改代碼。 – Neverland

+0

我知道唯一可行的方法就是使用祖先查詢。因此,例如,特定留言簿的每個問候語將該留言簿作爲其父母。是的,我同意這很煩人,幾個月前我遇到了同樣的問題......谷歌沒有從GAE應用程序中掩蓋這種情況,這有點愚蠢。 –

相關問題