Search code examples
pythongoogle-app-engineweb-applicationspython-2.7webapp2

Web application is not reloaded automatically since GAE migration from python 2.5.2 + webapp to 2.7 + webapp2


I tried to migrate the good old guestbook example from this google GAE tutorial video: http://www.youtube.com/watch?gl=DE&hl=de&v=bfgO-LXGpTM

Issue: when I jump to the main class via self.redirect('/'), the page is not reloaded automatically. I need to reload the browser window manually (e.g. via F5) to see the latest guestbook entry, which was done in the class MakeGuestbookEntry.

With python 2.5.2 + webapp this issue did not exist.

This is the python code file 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()

This is the html page 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>

Thanks for any help. Best Regards


Solution

  • That tutorial is quite old. I suggest you use the latest Guestbook tutorial in the Getting Started guide.

    The reason for this behavior, especially if you are in the dev server, is that GAE now simulates eventual consistency. Basically, this means that your newly added guestbook entry will not be present across all servers that your app is running on right away. Some of your users might see it instantly, some might not. A good way to make sure you're getting the latest data is to refresh the page and force the app to load it...but of course you can't expect users to like that :P

    The new guestbook tutorial uses ancestor queries instead, which enforces strong consistency. In other words, users will see updates right away, no need to refresh the page! You can read more about strong consistency here.