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

root/branches/cherrypy-3.0.x/cherrypy/tutorial/tut04_complex_site.py

Revision 1219 (checked in by fumanchu, 2 years ago)

Changed server.start to server.quickstart, and server.start_all to server.start.

  • Property svn:eol-style set to native
Line 
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     def index(self):
13         return '''
14             <p>Hi, this is the home page! Check out the other
15             fun stuff on this site:</p>
16             
17             <ul>
18                 <li><a href="/joke/">A silly joke</a></li>
19                 <li><a href="/links/">Useful links</a></li>
20             </ul>'''
21     index.exposed = True
22
23
24 class JokePage:
25     def index(self):
26         return '''
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>'''
30     index.exposed = True
31
32
33 class LinksPage:
34     def __init__(self):
35         # Request handler objects can create their own nested request
36         # handler objects. Simply create them inside their __init__
37         # methods!
38         self.extra = ExtraLinksPage()
39    
40     def index(self):
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
44         # links exclusively.
45         return '''
46             <p>Here are some useful links:</p>
47             
48             <ul>
49                 <li><a href="The">http://www.cherrypy.org">The CherryPy Homepage</a></li>
50                 <li><a href="The">http://www.python.org">The Python Homepage</a></li>
51             </ul>
52             
53             <p>You can check out some extra useful
54             links <a href="./extra/">here</a>.</p>
55             
56             <p>[<a href="../">Return</a>]</p>
57         '''
58     index.exposed = True
59
60
61 class ExtraLinksPage:
62     def index(self):
63         # Note the relative link back to the Links page!
64         return '''
65             <p>Here are some extra useful links:</p>
66             
67             <ul>
68                 <li><a href="del.icio.ushttp://del.icio.us">del.icio.us</a></li>
69                 <li><a href="Hendrik's">http://www.mornography.de">Hendrik's weblog</a></li>
70             </ul>
71             
72             <p>[<a href="../">Return to links page</a>]</p>'''
73     index.exposed = True
74
75
76 # Of course we can also mount request handler objects right here!
77 root = HomePage()
78 root.joke = JokePage()
79 root.links = LinksPage()
80 cherrypy.tree.mount(root)
81
82 # Remember, we don't need to mount ExtraLinksPage here, because
83 # LinksPage does that itself on initialization. In fact, there is
84 # no reason why you shouldn't let your root object take care of
85 # creating all contained request handler objects.
86
87
88 if __name__ == '__main__':
89     import os.path
90     cherrypy.config.update(os.path.join(os.path.dirname(__file__), 'tutorial.conf'))
91     cherrypy.server.quickstart()
92     cherrypy.engine.start()
93
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets