Package cherrypy :: Package tutorial :: Module tut03_get_and_post
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.tutorial.tut03_get_and_post

 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.tree.mount(WelcomePage()) 44 45 46 if __name__ == '__main__': 47 import os.path 48 thisdir = os.path.dirname(__file__) 49 cherrypy.quickstart(config=os.path.join(thisdir, 'tutorial.conf')) 50