.. _tutorials.gettingstarted.handlingforms: Handling Forms with webapp2 =========================== If we want users to be able to post their own greetings, we need a way to process information submitted by the user with a web form. The ``webapp2`` framework makes processing form data easy. Handling Web Forms With webapp2 ------------------------------- Replace the contents of ``helloworld/helloworld.py`` with the following:: import cgi from google.appengine.api import users import webapp2 class MainPage(webapp2.RequestHandler): def get(self): self.response.out.write("""
""") class Guestbook(webapp2.RequestHandler): def post(self): self.response.out.write('You wrote:') self.response.out.write(cgi.escape(self.request.get('content'))) self.response.out.write('') application = webapp2.WSGIApplication([ ('/', MainPage), ('/sign', Guestbook) ], debug=True) def main(): application.run() if __name__ == "__main__": main() Reload the page to see the form, then try submitting a message. This version has two handlers: ``MainPage``, mapped to the URL ``/``, displays a web form. ``Guestbook``, mapped to the URL ``/sign``, displays the data submitted by the web form. 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 (``method="post"``) to submit the form data. If for some reason you need a single handler to handle both GET and POST actions to the same URL, you can define a method for each action in the same class. The code for the ``post()`` method gets the form data from ``self.request``. Before displaying it back to the user, it uses ``cgi.escape()`` to escape HTML special characters to their character entity equivalents. ``cgi`` is a module in the standard Python library; see `the documentation for cgi