Handling User Input in a Form

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


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 guestbook.py as the handler for all URLs:

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

- url: /bootstrap
  static_dir: bootstrap

- url: /.*
  script: guestbook.app

Defining a handler for form submission

The app object in guestbook.py 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):
        # 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():
            greeting.author = 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.

هل كانت هذه الصفحة مفيدة؟ يرجى تقييم أدائنا:

إرسال تعليقات حول...

App Engine standard environment for Python 2