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

root/branches/cherrypy-2.x/cherrypy/tutorial/tut03_get_and_post.py

Revision 762 (checked in by fumanchu, 3 years ago)

Set svn:eol-style to "native" for all .py files. This should fix any line-ending problems for existing files: Windows users will receive CRLF endings when they check out files, and *nix users will receive LF endings. Whenever you "svn add" a new file, make sure its svn:eol-style property is "native"!

  • 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 cherrypy.root = WelcomePage()
44
45
46 if __name__ == '__main__':
47     cherrypy.config.update(file = 'tutorial.conf')
48     cherrypy.server.start()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets