1

この Google GAE チュートリアル ビデオから古き良きゲストブックの例を移行しようとしました: http://www.youtube.com/watch?gl=DE&hl=de&v=bfgO-LXGpTM

問題: self.redirect('/')経由でメイン クラスにジャンプすると、ページが自動的にリロードされません。MakeGuestbookEntry クラスで行われた最新のゲストブック エントリを表示するには、ブラウザー ウィンドウを手動で (F5 などを使用して) 再読み込みする必要があります。

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>

助けてくれてありがとう。よろしくお願いします

4

1 に答える 1

1

そのチュートリアルはかなり古いです。Getting Started guideの最新の Guestbook チュートリアルを使用することをお勧めします。

この動作の理由は、特に開発サーバーにいる場合、GAE が結果整合性をシミュレートするようになったためです。基本的に、これは、新しく追加されたゲストブック エントリが、アプリがすぐに実行されているすべてのサーバーに存在しないことを意味します。すぐに表示されるユーザーもいれば、表示されないユーザーもいます。最新のデータを取得していることを確認する良い方法は、ページを更新してアプリに強制的にロードさせることです...しかし、もちろん、ユーザーがそれを気に入ってくれるとは期待できません:P

新しいゲストブック チュートリアルでは代わりに祖先クエリを使用しており、これにより強い整合性が適用されます。つまり、ユーザーはすぐに更新を確認でき、ページを更新する必要はありません! 強整合性の詳細については、こちらを参照してください。

于 2013-10-03T00:25:50.857 に答える