1.. _tutorials.gettingstarted.handlingforms:
2
3Handling Forms with webapp2
4===========================
5If we want users to be able to post their own greetings, we need a way to
6process information submitted by the user with a web form. The ``webapp2``
7framework makes processing form data easy.
8
9
10Handling Web Forms With webapp2
11-------------------------------
12Replace the contents of ``helloworld/helloworld.py`` with the following::
13
14    import cgi
15
16    from google.appengine.api import users
17    import webapp2
18
19    class MainPage(webapp2.RequestHandler):
20        def get(self):
21            self.response.out.write("""
22              <html>
23                <body>
24                  <form action="/sign" method="post">
25                    <div><textarea name="content" rows="3" cols="60"></textarea></div>
26                    <div><input type="submit" value="Sign Guestbook"></div>
27                  </form>
28                </body>
29              </html>""")
30
31    class Guestbook(webapp2.RequestHandler):
32        def post(self):
33            self.response.out.write('<html><body>You wrote:<pre>')
34            self.response.out.write(cgi.escape(self.request.get('content')))
35            self.response.out.write('</pre></body></html>')
36
37    application = webapp2.WSGIApplication([
38        ('/', MainPage),
39        ('/sign', Guestbook)
40    ], debug=True)
41
42    def main():
43        application.run()
44
45    if __name__ == "__main__":
46        main()
47
48Reload the page to see the form, then try submitting a message.
49
50This version has two handlers: ``MainPage``, mapped to the URL ``/``, displays
51a web form. ``Guestbook``, mapped to the URL ``/sign``, displays the data
52submitted by the web form.
53
54The ``Guestbook`` handler has a ``post()`` method instead of a ``get()``
55method. This is because the form displayed by ``MainPage`` uses the HTTP POST
56method (``method="post"``) to submit the form data. If for some reason you
57need a single handler to handle both GET and POST actions to the same URL, you
58can define a method for each action in the same class.
59
60The code for the ``post()`` method gets the form data from ``self.request``.
61Before displaying it back to the user, it uses ``cgi.escape()`` to escape
62HTML special characters to their character entity equivalents. ``cgi`` is a
63module in the standard Python library; see `the documentation for cgi <http://www.python.org/doc/2.5.2/lib/module-cgi.html>`_
64for more information.
65
66.. note::
67   The App Engine environment includes the entire Python 2.5 standard library.
68   However, not all actions are allowed. App Engine applications run in a
69   restricted environment that allows App Engine to scale them safely.
70   For example, low-level calls to the operating system, networking operations,
71   and some filesystem operations are not allowed, and will raise an error
72   when attempted. For more information, see `The Python Runtime Environment <http://code.google.com/appengine/docs/python/>`_.
73
74
75Next...
76-------
77Now that we can collect information from the user, we need a place to put it
78and a way to get it back.
79
80Continue to :ref:`tutorials.gettingstarted.usingdatastore`.
81