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

Source Code for Module cherrypy.tutorial.tut04_complex_site

  1  """ 
  2  Tutorial - Multiple objects 
  3   
  4  This tutorial shows you how to create a site structure through multiple 
  5  possibly nested request handler objects. 
  6  """ 
  7   
  8  import cherrypy 
  9   
 10   
11 -class HomePage:
12
13 - def index(self):
14 return ''' 15 <p>Hi, this is the home page! Check out the other 16 fun stuff on this site:</p> 17 18 <ul> 19 <li><a href="/joke/">A silly joke</a></li> 20 <li><a href="/links/">Useful links</a></li> 21 </ul>'''
22 index.exposed = True
23 24
25 -class JokePage:
26
27 - def index(self):
28 return ''' 29 <p>"In Python, how do you create a string of random 30 characters?" -- "Read a Perl file!"</p> 31 <p>[<a href="../">Return</a>]</p>'''
32 index.exposed = True
33 34
35 -class LinksPage:
36
37 - def __init__(self):
38 # Request handler objects can create their own nested request 39 # handler objects. Simply create them inside their __init__ 40 # methods! 41 self.extra = ExtraLinksPage()
42
43 - def index(self):
44 # Note the way we link to the extra links page (and back). 45 # As you can see, this object doesn't really care about its 46 # absolute position in the site tree, since we use relative 47 # links exclusively. 48 return ''' 49 <p>Here are some useful links:</p> 50 51 <ul> 52 <li> 53 <a href="http://www.cherrypy.org">The CherryPy Homepage</a> 54 </li> 55 <li> 56 <a href="http://www.python.org">The Python Homepage</a> 57 </li> 58 </ul> 59 60 <p>You can check out some extra useful 61 links <a href="./extra/">here</a>.</p> 62 63 <p>[<a href="../">Return</a>]</p> 64 '''
65 index.exposed = True
66 67
68 -class ExtraLinksPage:
69
70 - def index(self):
71 # Note the relative link back to the Links page! 72 return ''' 73 <p>Here are some extra useful links:</p> 74 75 <ul> 76 <li><a href="http://del.icio.us">del.icio.us</a></li> 77 <li><a href="http://www.cherrypy.org">CherryPy</a></li> 78 </ul> 79 80 <p>[<a href="../">Return to links page</a>]</p>'''
81 index.exposed = True
82 83 84 # Of course we can also mount request handler objects right here! 85 root = HomePage() 86 root.joke = JokePage() 87 root.links = LinksPage() 88 89 # Remember, we don't need to mount ExtraLinksPage here, because 90 # LinksPage does that itself on initialization. In fact, there is 91 # no reason why you shouldn't let your root object take care of 92 # creating all contained request handler objects. 93 94 95 import os.path 96 tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') 97 98 if __name__ == '__main__': 99 # CherryPy always starts with app.root when trying to map request URIs 100 # to objects, so we need to mount a request handler root. A request 101 # to '/' will be mapped to HelloWorld().index(). 102 cherrypy.quickstart(root, config=tutconf) 103 else: 104 # This branch is for the test suite; you can ignore it. 105 cherrypy.tree.mount(root, config=tutconf) 106