【问题标题】:Web application is not reloaded automatically since GAE migration from python 2.5.2 + webapp to 2.7 + webapp2由于 GAE 从 python 2.5.2 + webapp 迁移到 2.7 + webapp2,Web 应用程序不会自动重新加载
【发布时间】:2013-10-07 15:56:59
【问题描述】:

我尝试从这个 google GAE 教程视频迁移好的旧留言簿示例: http://www.youtube.com/watch?gl=DE&hl=de&v=bfgO-LXGpTM

问题:当我通过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>

感谢您的帮助。 最好的问候

【问题讨论】:

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


    【解决方案1】:

    那个教程很老了。我建议你使用Getting Started guide中最新的Guestbook教程。

    这种行为的原因,尤其是如果您在开发服务器中,是因为 GAE 现在模拟了最终一致性。基本上,这意味着您新添加的留言簿条目不会立即出现在您的应用程序正在运行的所有服务器上。您的一些用户可能会立即看到它,而另一些可能不会。确保您获得最新数据的一个好方法是刷新页面并强制应用程序加载它......但当然你不能指望用户喜欢这样:P

    新的留言板教程改为使用祖先查询,它强制执行强一致性。换句话说,用户将立即看到更新,无需刷新页面!你可以阅读更多关于强一致性的内容here

    【讨论】:

    • 非常感谢!这导致了“问题”。这不是一个真正的问题,因为它按设计工作。但在我看来,这使得以前简单易用的系统变得复杂了。您知道有什么方法可以在不大量更改源代码的情况下强制使用强一致性吗?我试图理解谷歌的解释,但我无法以正常工作的方式更改代码。
    • 我知道唯一可以确定的方法是使用祖先查询。因此,例如,特定留言簿的每个问候语都将该留言簿作为其父级。是的,我同意这很烦人,几个月前我遇到了同样的问题......谷歌没有从 GAE 应用程序中掩盖这一点有点愚蠢。
    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多