1

My app, Datastore, webapp2, and form-specific "responses" are all working :) but I need the page to load without displaying previous visitor's query results. I need query results only for current form-submitter, after they submit form. Is this a session or headers solution, or can I edit the GqlQuery to accomplish this?

messages = db.GqlQuery("SELECT * "
                                "FROM Visitor " 
                                "ORDER BY date DESC LIMIT 1") #obviously shows previous form submit
        for message in messages:
            if message.name == "" or message.mood == "":
                self.response.out.write("<div class='textright'>Type name and select.</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  

^ ^ this last section is my "response" that needs to appear only after current visitor submits form.

p1nesap
  • 179
  • 1
  • 3
  • 13

2 Answers2

1

I would strongly recommend you to go through the Getting Started and especially the templates section, until you will understand how it works.

But you if you just want to see your example in action try this (read more):

class Process(webapp.RequestHandler):
  def post(self):
    name = self.request.get("name")
    mood = self.request.get("mood")

    if mood == "bad" and name != "": 
      self.response.out.write("<html><body>")
      self.response.out.write("<h1>Welcome to the Internet!</h1>")
      self.response.out.write("<p>My mood is %s and my name is %s</p>" % (mood, name))
      self.response.out.write("</body></html>")
    else:
      self.response.out.write("<html><body>")
      self.response.out.write("<h1>Welcome to the Internet anyway!</h1>")
      self.response.out.write("</body></html>")

Also never use print in your GAE applications, use the logger instead for debugging and more.

Lipis
  • 21,388
  • 20
  • 94
  • 121
  • Lipis, thank you for the code and links. I think I was close. I was under impression print is for non-class .py scripts, and that s.r.o.w. is for classes? There are so many pages in the GAE/API space, it helps to have someone link to exact doc. – p1nesap Jun 19 '12 at 22:57
  • The above code has nothing to do with applying stuff in the datastore.. You have to go through the Getting Started maybe at least twice.. and read each section http://goo.gl/rRucs – Lipis Jun 20 '12 at 10:19
  • @pavl change it to `/process/` with the forward slash in the end... because most likely you have `action="."` in your `
    ` and not the full url...
    – Lipis Jun 20 '12 at 13:16
  • 1
    @pavl I think there is no point to try to "just" make it work.. and you will have to understand exactly what is what. What is datastore, how the form works, what is the POST request, what is the GET request and what is action in the form.. you're touching lots of different areas in the same question and it's hard to understand the problem..! make sure you went through the getting started.. at least twice from scratch.. – Lipis Jun 20 '12 at 16:02
  • thanks, I have replicated both guestbook examples including jinja template...now I will try to apply concepts to my script. will report back. – p1nesap Jun 20 '12 at 19:40
  • Regarding print, it's straightforward: Never ever use print inside a WSGI app. – Nick Johnson Jun 20 '12 at 22:33
  • Could I get some help with my revised code above please? Due to my poor rating I cannot post new questions. – p1nesap Jun 23 '12 at 01:46
  • @pavl that's not the way to get help though.. did you ever wondered why your rating is so low? Maybe you should improve the quality of your questions... on how to post in a better way.. Did you go through the FAQ of stack overflow...? This is also one good start: http://blog.stackoverflow.com/2010/10/asking-better-questions/ – Lipis Jun 23 '12 at 14:21
  • @pavl As for your question this is still no the way to accomplish this thing.. you should use templates.. and understand them how they work..! In real life you will never have to `self.response.out.write` HTML snippets.. use templates! – Lipis Jun 23 '12 at 14:23
  • @pavl Also updating the question, is not going to help anyone.. because all the answers will become irrelevant..!! And the whole thing will be transformed to junk and that's one of the reasons that your score is not going up! – Lipis Jun 23 '12 at 14:26
  • Thanks for responses Lipis. You're a good guy. I took your advice to accept answers, and have phrased and edited questions to the best of my ability, but apparently that is inadequate for this forum. I love using Google applications, but unfortunately the passage from enthusiast to Python/GAE coder seems difficult, given the denial I've experienced here, Google's recommended forum. I have made headway working through the tutorial, but now I am at an impasse, as my question shows. I will follow your templates suggestion. Strange that Python/GAE requires a template to do something so basic. – p1nesap Jun 24 '12 at 11:59
  • @pavl Stack overflow is not attacking you personally.. http://meta.stackexchange.com/a/128589/142717 What you are asking is doable in GAE but is not worth it.. trust me.. it doesn't scale very well. Think about it if you want to start reusing some forms.. or have the same theme on all pages.. Read more on templates and try to reuse them in your project like in the Getting Started guide.. – Lipis Jun 24 '12 at 12:11
  • @ Lipis, thanks, I will follow advice on templates...jinja2 is the only one with an example for Python27, so I'll use that. I wanted to try Django 1.2, but Google provides no example for Python27, and following example provided for Django/Python25 does not look like it would work with Django1.2/Python25. Is Django no longer relevant? – p1nesap Jun 24 '12 at 15:04
  • @pavl it is relevant.. But jinja2 is more or less the same, and in my opinion I think is better than Django.. check the docs and figure out how it works.. there are no differences in the basic stuff (includes, extends, for-loop, if-statements, etc..) – Lipis Jun 24 '12 at 15:42
1

If you want to emit values for debugging purposes, particularly if you want that before an <html> tag is written, try

self.response.out.write("<!-- name: %s -->" % self.request.get("name"))

Otherwise, the browser might get confused.

print from a handler will never to what you expect.

In your snippet, you haven't shown where var7 and var9 come from.

I do realize that post/.put form values to Datastore automatically redirects user to new page

I think you misunderstand. You haven't shown us where your code does a put() or a redirect. A post() handler does not automatically do either.

Which tutorial are you looking at? Perhaps we need to tighten up vague wording.

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46