| 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 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
self.extra = ExtraLinksPage() |
|---|
| 39 |
|
|---|
| 40 |
def index(self): |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 77 |
root = HomePage() |
|---|
| 78 |
root.joke = JokePage() |
|---|
| 79 |
root.links = LinksPage() |
|---|
| 80 |
cherrypy.tree.mount(root) |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 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 |
|
|---|