Question

I am trying to create a voting button for posts using Google App Engine. Currently I am implementing it like this:

class Latest(Handler):

    def get(self):
        posts = recent_posts()
        qt = memcache.get('recent_posts_qt')
        if qt:
            qt = time.time() - qt
        self.render('latest.html', articles = posts, qt = qt)

    def post(self):
        post_id = int(self.request.get("id"))
        q = AllPost.get_by_id(post_id)
        q.votes += 1
        q.put()
        time.sleep(0.5)
        update = recent_posts(update=True) # for memcache
        posts = db.GqlQuery("SELECT * FROM AllPost ORDER BY created DESC LIMIT 10")
        posts = list(posts)
        self.render('latest.html', articles=posts)

The html I am using is this:

<div class="article-votes">Votes: {{item.votes}}
    <form action="/latest" method="post">
        <input name="id" value={{item.key().id()}}>
        <button>Vote+</button>
    </form>
</div>

If I try to refresh the page after voting on a post, I get the "confirm form submission alert". There must be a better way to implement this without this happening. Is it possible to update the vote count and datastore without rendering the page again?

Was it helpful?

Solution

Refreshing a POST request will always trigger that confirmation window, it is a common behavior in most browsers. You can control the form POST request closely via Javascript using AJAX (XMLHTTPRequest) and that wouldn't cause another page render.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top