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

root/trunk/cherrypy/tutorial/tut03_get_and_post.py

Revision 2566 (checked in by fumanchu, 2 months ago)

Fixing tutorials which did not run on command-line.

  • Property svn:eol-style set to native
Line 
1 """
2 Tutorial - Passing variables
3
4 This tutorial shows you how to pass GET/POST variables to methods.
5 """
6
7 import cherrypy
8
9
10 class WelcomePage:
11
12     def index(self):
13         # Ask for the user's name.
14         return '''
15             <form action="greetUser" method="GET">
16             What is your name?
17             <input type="text" name="name" />
18             <input type="submit" />
19             </form>'''
20     index.exposed = True
21    
22     def greetUser(self, name = None):
23         # CherryPy passes all GET and POST variables as method parameters.
24         # It doesn't make a difference where the variables come from, how
25         # large their contents are, and so on.
26         #
27         # You can define default parameter values as usual. In this
28         # example, the "name" parameter defaults to None so we can check
29         # if a name was actually specified.
30        
31         if name:
32             # Greet the user!
33             return "Hey %s, what's up?" % name
34         else:
35             if name is None:
36                 # No name was specified
37                 return 'Please enter your name <a href="./">here</a>.'
38             else:
39                 return 'No, really, enter your name <a href="./">here</a>.'
40     greetUser.exposed = True
41
42
43 import os.path
44 tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
45
46 if __name__ == '__main__':
47     # CherryPy always starts with app.root when trying to map request URIs
48     # to objects, so we need to mount a request handler root. A request
49     # to '/' will be mapped to HelloWorld().index().
50     cherrypy.quickstart(WelcomePage(), config=tutconf)
51 else:
52     # This branch is for the test suite; you can ignore it.
53     cherrypy.tree.mount(WelcomePage(), config=tutconf)
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets