Robert Dodd
Robert Dodd

Reputation: 2177

App Engine: map form data to url

How do I encode form data to the url when using the Google App Engine?

The code below produces a form where you enter some text. The text is then displayed on another page "/sign"

Instead of directing to /sign I would like to direct to /sign?message="hello"

Here is my code, from here: https://developers.google.com/appengine/docs/python/gettingstartedpython27/handlingforms)

import cgi
import webapp2

from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("""
          <html>
            <body>
              <form action="/sign" method="post">
                <div><textarea name="content" rows="3" cols="60"></textarea></div>
                <div><input type="submit" value="Sign Guestbook"></div>
              </form>
            </body>
          </html>""")


class Guestbook(webapp2.RequestHandler):
    def post(self):
        self.response.out.write('<html><body>You wrote:<pre>')
        self.response.out.write(cgi.escape(self.request.get('content')))
        self.response.out.write('</pre></body></html>')

app = webapp2.WSGIApplication([('/', MainPage),
                              ('/sign', Guestbook)],
                              debug=True)

Upvotes: 1

Views: 152

Answers (1)

Drew Verlee
Drew Verlee

Reputation: 1900

I'm not enterally sure what your trying to achieve. You might get more solid advice if you wrote about your intentions.

To get the result I think your describing you will need to use get instead of post. Get will alter the url. Here is the modified code:

import cgi
import webapp2

from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("""
          <html>
            <body>
              <form action="/sign">
                <div><textarea name="content" rows="3" cols="60"></textarea></div>
                <div><input type="submit" value="Sign Guestbook"></div>
              </form>
            </body>
          </html>""")


class Guestbook(webapp2.RequestHandler):
    def get(self):
        self.response.out.write('<html><body>You wrote:<pre>')
        self.response.out.write(cgi.escape(self.request.get('content')))
        self.response.out.write('</pre></body></html>')


app = webapp2.WSGIApplication([('/', MainPage),
                              ('/sign', Guestbook)],
                              debug=True)

If your looking to learn more about webapp2 and the get and post method check out udacity CS253

Upvotes: 1

Related Questions