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

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

Revision 762 (checked in by fumanchu, 3 years ago)

Set svn:eol-style to "native" for all .py files. This should fix any line-ending problems for existing files: Windows users will receive CRLF endings when they check out files, and *nix users will receive LF endings. Whenever you "svn add" a new file, make sure its svn:eol-style property is "native"!

  • 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 cherrypy.root = HomePage()
78 cherrypy.root.joke = JokePage()
79 cherrypy.root.links = LinksPage()
80
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.
85
86
87 if __name__ == '__main__':
88     cherrypy.config.update(file = 'tutorial.conf')
89     cherrypy.server.start()
90
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets