Learning objectives
- Learn how to build and deploy an App Engine app, a simple guestbook
Prerequisites
- Basic familiarity with Python
- PC, Mac, or Linux computer with Python 2.7 installed
- The Introduction to App Engine class
Related
- Google App Engine home page
- App Engine Python documentation
- Google App Engine blog
- Python home page
- App Engine 101 in Python, Part II : the successor to this class
Amy Unruh, Dan Sanderson, Oct 2012
Google Developer Relations
Introduction
For users to be able to post their own guestbook messages, you need a way to process information submitted by the user with a web form. This lesson shows how to do that using
webapp2
.
Handling Web Forms with webapp2
The
webapp2
framework makes processing form data easy. Replace the contents of
helloworld/main.py
with the following:
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)
Reload the page to see the form, then try submitting a message.
This new version of
main.py
now 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 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 Python standard library; see the
library documentation
for more information.
Note: The App Engine environment includes the entire Python 2.7 standard library. However, not all actions are allowed. App Engine applications run in a restricted environment that allows App Engine to scale them safely. For example, low-level calls to the operating system, networking operations, and some file system operations are not allowed, and will raise an error when attempted. For more information, see The Python Runtime Environment .
Summary and Review
This lesson has shown how to use the
webapp2
framework to support form processing. To test your understanding, try changing the form to include an additional text field (for example, the user's country), and then collect and write out the value of that new field as well.
Now that you can collect information from the user, you need a place to store it and a way to get it back. We'll look at how to do that in the class that follows this one, App Engine 101 in Python, Part II .