2 Tutorial - Multiple objects
4 This tutorial shows you how to create a site structure through multiple
5 possibly nested request handler objects.
14 <p>Hi, this is the home page! Check out the other
15 fun stuff on this site:</p>
18 <li><a href="/joke/">A silly joke</a></li>
19 <li><a href="/links/">Useful links</a></li>
27 <p>"In Python, how do you create a string of random
28 characters?" -- "Read a Perl file!"</p>
29 <p>[<a href="../">Return</a>]</p>'''
35 # Request handler objects can create their own nested request
36 # handler objects. Simply create them inside their __init__
38 self
.extra
= ExtraLinksPage()
41 # Note the way we link to the extra links page (and back).
42 # As you can see, this object doesn't really care about its
43 # absolute position in the site tree, since we use relative
46 <p>Here are some useful links:</p>
49 <li><a href="http://www.cherrypy.org">The CherryPy Homepage</a></li>
50 <li><a href="http://www.python.org">The Python Homepage</a></li>
53 <p>You can check out some extra useful
54 links <a href="./extra/">here</a>.</p>
56 <p>[<a href="../">Return</a>]</p>
63 # Note the relative link back to the Links page!
65 <p>Here are some extra useful links:</p>
68 <li><a href="http://del.icio.us">del.icio.us</a></li>
69 <li><a href="http://www.mornography.de">Hendrik's weblog</a></li>
72 <p>[<a href="../">Return to links page</a>]</p>'''
76 # Of course we can also mount request handler objects right here!
78 root
.joke
= JokePage()
79 root
.links
= LinksPage()
81 # Remember, we don't need to mount ExtraLinksPage here, because
82 # LinksPage does that itself on initialization. In fact, there is
83 # no reason why you shouldn't let your root object take care of
84 # creating all contained request handler objects.
88 tutconf
= os
.path
.join(os
.path
.dirname(__file__
), 'tutorial.conf')
90 if __name__
== '__main__':
91 # CherryPy always starts with app.root when trying to map request URIs
92 # to objects, so we need to mount a request handler root. A request
93 # to '/' will be mapped to HelloWorld().index().
94 cherrypy
.quickstart(root
, config
=tutconf
)
96 # This branch is for the test suite; you can ignore it.
97 cherrypy
.tree
.mount(root
, config
=tutconf
)