Display Gql Query Results Only for Session?

48 views
Skip to first unread message

paulmo

unread,
Jun 23, 2012, 7:06:34 AM6/23/12
to google-a...@googlegroups.com
I am new to GAE, have read tutorial and followed the guestbook example to apply to my app. I've tried to communicate on Stackoverflow, but due to negative rating, I cannot ask my question there. Please help here.

I have responses that display on page when site visitor submits form. Submitted form values are stored in Datastore. Responses and Datastore all work great except when a visitor goes to my page, the previous response is already displayed because the query seems to execute when the page loads...but I need response to display only immediately after form submit, and not carry over to subsequent page visits. Is this achieved through editing the query, session, headers? If so please lead me to the solution.

messages = db.GqlQuery("SELECT * "
                                "FROM Visitor "  #Visitor is Datastore entity; properties are name, mood, date.
                                "ORDER BY date DESC LIMIT 1") 
            for message in messages:
            if message.name and message.mood == "": #give warning if user doesn't fill out form; of course this too is displaying on page load, but I need it to display only if user doesn't fill out form. help? v v
                self.response.out.write("<body><html>")
                self.response.out.write("<div class='textright'>Type your first name and select mood.</div>")
                self.response.out.write("</body></html>")
            elif message.mood == "bad" and message.name != "":
                self.response.out.write("<body><html>")
                self.response.out.write("<div class='textright'>Stay the course %s...# more if/elifs responses follow based on mood value; these are the "responses" that I need to display only right after form submit, not when page loads with new visitor, etc.

            self.response.out.write("""<html><body>       
<form action="/process" method="post">
<p>First Name: <input type="text" name="name"/></p>
<p><input type="radio" name="mood" value="good">Good</p>
<p><input type="radio" name="mood" value="bad">Bad</p>
<p><input type="radio" name="mood" value="fair">Fair</p>
<p><input type="submit" value="Process"/></p>
</form>
</body></html> """) 

class Process(webapp2.RequestHandler):
    def post(self):
        name = self.request.get("name")
        mood = self.request.get("mood")
        message = Visitor(name=name, mood=mood)  #Visitor is entity (from class Visitor db.Model)
        if users.get_current_user():                             #tried this but probably not necessary; no log-in required.
            message.name = users.get_current_user()
        message.mood = self.request.get("mood")
        message.put()
       
        self.redirect("/")

Michael Hermus

unread,
Jun 25, 2012, 4:48:03 PM6/25/12
to google-a...@googlegroups.com
Unfortunately, this is not a great design for any sort of web application, GAE or otherwise; you may be in a bit over your head.

The most basic version of this should probably have two separate pages: the 'mood form' and the 'response page'. When the user submits the mood form, you can store the Visitor entity in the handler, and immediately get the Entity key value. You can then redirect the user to the 'response page' and pass the key value of the Visitor entity as a parameter. The 'response page' could then retrieve the Visitor entity and do whatever processing is needed to display the appropriate response. You could also store the key value as long lived cookie in the user's browser, so that the response would show up on subsequent visits.


Reply all
Reply to author
Forward
0 new messages