Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products. If you have feedback or questions as you navigate the site, click Send Feedback.

Python 2 is no longer supported by the community. We recommend that you migrate Python 2 apps to Python 3.

Handling User Input in a Form

Stay organized with collections Save and categorize content based on your preferences.

This part of the Python Guestbook code walkthrough shows how to handle user input.

This page is part of a multi-page tutorial. To start from the beginning and see instructions for setting up, go to Creating a Guestbook.

Configuring the app to use webapp2

The Guestbook sample uses the webapp2 framework, which is included in the App Engine environment and the App Engine Python SDK. You don't need to bundle webapp2 with your application code to use it.

The app.yaml file specifies that the app uses the webapp2 framework:

- name: webapp2
  version: latest
- name: jinja2
  version: latest

A webapp2 application has two parts:

  • One or more RequestHandler classes that process requests and build responses.
  • A WSGIApplication instance that routes incoming requests to handlers based on the URL.

The app.yaml file specifies the app object in as the handler for all URLs:

- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /bootstrap
  static_dir: bootstrap

- url: /.*

Defining a handler for form submission

The app object in is a WSGIApplication that defines which scripts handle requests for given URLs.

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

The debug=True parameter tells webapp2 to print stack traces to the browser output if a handler encounters an error or raises an uncaught exception. This option should be removed before deploying the final version of your application, otherwise you will inadvertently expose the internals of your application.

The Guestbook handler has a post() method instead of a get() method. This is because the form displayed by MainPage uses the HTTP POST method to submit the form data.

class Guestbook(webapp2.RequestHandler):

    def post(self):
        # We set the same parent key on the 'Greeting' to ensure each
        # Greeting is in the same entity group. Queries across the
        # single entity group will be consistent. However, the write
        # rate to a single entity group should be limited to
        # ~1/second.
        guestbook_name = self.request.get('guestbook_name',
        greeting = Greeting(parent=guestbook_key(guestbook_name))

        if users.get_current_user():
   = Author(

        greeting.content = self.request.get('content')

        query_params = {'guestbook_name': guestbook_name}
        self.redirect('/?' + urllib.urlencode(query_params))

The post() method gets the form data from self.request.