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

root/tags/cherrypy-2.0.0/cherrypy/tutorial/04_complex_site.py

Revision 102 (checked in by dpotter, 4 years ago)

add descriptive names to the tutorials

Line 
1 """
2 Tutorial 04 - 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 from cherrypy import cpg
9
10 class HomePage:
11     def index(self):
12         return '''
13             <p>Hi, this is the home page! Check out the other
14             fun stuff on this site:</p>
15
16             <ul>
17                 <li><a href="/joke/">A silly joke</a></li>
18                 <li><a href="/links/">Useful links</a></li>
19             </ul>
20         '''
21
22     index.exposed = True
23
24
25 class JokePage:
26     def index(self):
27         return '''
28             <p>"In Python, how do you create a string of random
29             characters?" -- "Read a Perl file!"</p>
30             <p>[<a href="../">Return</a>]</p>
31         '''
32
33     index.exposed = True
34
35
36 class LinksPage:
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><a href="The">http://www.cherrypy.org">The CherryPy Homepage</a></li>
53                 <li><a href="The">http://www.python.org">The Python Homepage</a></li>
54             </ul>
55
56             <p>You can check out some extra useful
57             links <a href="./extra/">here</a>.</p>
58
59             <p>[<a href="../">Return</a>]</p>
60         '''
61
62     index.exposed = True
63
64
65 class ExtraLinksPage:
66     def index(self):
67         # Note the relative link back to the Links page!
68         return '''
69             <p>Here are some extra useful links:</p>
70
71             <ul>
72                 <li><a href="del.icio.ushttp://del.icio.us">del.icio.us</a></li>
73                 <li><a href="Hendrik's">http://www.mornography.de">Hendrik's weblog</a></li>
74             </ul>
75
76             <p>[<a href="../">Return to links page</a>]</p>
77         '''
78
79     index.exposed = True
80
81
82 # Of course we can also mount request handler objects right here!
83 cpg.root = HomePage()
84 cpg.root.joke = JokePage()
85 cpg.root.links = LinksPage()
86
87 # Remember, we don't need to mount ExtraLinksPage here, because
88 # LinksPage does that itself on initialization. In fact, there is
89 # no reason why you shouldn't let your root object take care of
90 # creating all contained request handler objects.
91
92 cpg.server.start(configFile = 'tutorial.conf')
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets