Reputation: 173
I'm very new to google app engine and i'm trying this guestbook google code example. What is my problem is when i post a post it doesn't displays it but when i post the second post it displays the first post. It seems like the page is needed to be refreshed at the first time and seems like this code "self.redirect('/')" work a step back ahead of the program. Or there is a different problem please help me. I don't know if this is about my computer but i would be appreciated if you could help me.
And here is the original code:
import cgi
import os
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template
class Greeting(db.Model):
author = db.UserProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
class MainPage(webapp.RequestHandler):
def get(self):
greetings_query = Greeting.all().order('-date')
greetings = greetings_query.fetch(10)
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'greetings': greetings,
'url': url,
'url_linktext': url_linktext,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
class Guestbook(webapp.RequestHandler):
def post(self):
greeting = Greeting()
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get('content')
greeting.put()
self.redirect('/')
application = webapp.WSGIApplication(
[('/', MainPage),
('/sign', Guestbook)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Upvotes: 0
Views: 102
Reputation: 14213
I don't see that there is anything wrong with this code, so I am inclined to think that your browser may be caching the main page and so displaying results without actually making a call to your AppEngine site. I'd suggest modifying the redirect by adding a parameter at the end of the URL that will cause your browser to make a call to the server. Here's an approach:
Add import time
to the imports at the top of the file.
Change the end of the post
method of Guestbook
to read:
timestamp = str(time.time()).replace(".","")
self.redirect('/?cachebuster=' + timestamp)
The GET query parameter value that is added to the URL will make sure that your browser makes a new actual request to the server.
EDIT: Silly me. I think that what you are seeing is the effect of eventual consistency. In short: Google's High-replication Datastore stores your app's data in multiple datacenters, but you have to take special measures to guarantee that when you do a query that you get strongly consistent results; otherwise, the datacenter that serves your query might not have received the update with the new data yet.
It is all very well documented in the AppEngine Developer Docs: https://developers.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency
Upvotes: 1