Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/tags/cherrypy-2.0.0/cherrypy/tutorial/03_get_and_post.py

Revision 102 (checked in by dpotter, 4 years ago)

add descriptive names to the tutorials

Line 
1 """
2 Tutorial 03 - Passing variables
3
4 This tutorial shows you how to pass GET/POST variables to methods.
5 """
6
7 from cherrypy import cpg
8
9 class WelcomePage:
10
11     def index(self):
12         # Ask for the user's name.
13         return '''
14             <form action="greetUser" method="GET">
15             What is your name?
16             <input type="text" name="name" />
17             <input type="submit" />
18             </form>
19         '''
20
21     index.exposed = True
22
23
24     def greetUser(self, name = None):
25         # CherryPy passes all GET and POST variables as method parameters.
26         # It doesn't make a difference where the variables come from, how
27         # large their contents are, and so on.
28         #
29         # You can define default parameter values as usual. In this
30         # example, the "name" parameter defaults to None so we can check
31         # if a name was actually specified.
32
33         if name:
34             # Greet the user!
35             return "Hey %s, what's up?" % name
36         else:
37             # No name was specified
38             return 'Please enter your name <a href="./">here</a>.'
39
40     greetUser.exposed = True
41
42
43 cpg.root = WelcomePage()
44 cpg.server.start(configFile = 'tutorial.conf')
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets