|
Revision 1837
(checked in by fumanchu, 6 months ago)
|
Fix for #756 (Deprecate server.quickstart):
- server.quickstart now does nothing but raise a warning.
- Made 'root' argument to cherrypy.quickstart optional (to make tutorials easier, but it applies broadly).
- Removed all calls to server.quickstart.
|
- Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 |
""" |
|---|
| 2 |
Tutorial - Object inheritance |
|---|
| 3 |
|
|---|
| 4 |
You are free to derive your request handler classes from any base |
|---|
| 5 |
class you wish. In most real-world applications, you will probably |
|---|
| 6 |
want to create a central base class used for all your pages, which takes |
|---|
| 7 |
care of things like printing a common page header and footer. |
|---|
| 8 |
""" |
|---|
| 9 |
|
|---|
| 10 |
import cherrypy |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
class Page: |
|---|
| 14 |
|
|---|
| 15 |
title = 'Untitled Page' |
|---|
| 16 |
|
|---|
| 17 |
def header(self): |
|---|
| 18 |
return ''' |
|---|
| 19 |
<html> |
|---|
| 20 |
<head> |
|---|
| 21 |
<title>%s</title> |
|---|
| 22 |
<head> |
|---|
| 23 |
<body> |
|---|
| 24 |
<h2>%s</h2> |
|---|
| 25 |
''' % (self.title, self.title) |
|---|
| 26 |
|
|---|
| 27 |
def footer(self): |
|---|
| 28 |
return ''' |
|---|
| 29 |
</body> |
|---|
| 30 |
</html> |
|---|
| 31 |
''' |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
class HomePage(Page): |
|---|
| 41 |
|
|---|
| 42 |
title = 'Tutorial 5' |
|---|
| 43 |
|
|---|
| 44 |
def __init__(self): |
|---|
| 45 |
|
|---|
| 46 |
self.another = AnotherPage() |
|---|
| 47 |
|
|---|
| 48 |
def index(self): |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
return self.header() + ''' |
|---|
| 52 |
<p> |
|---|
| 53 |
Isn't this exciting? There's |
|---|
| 54 |
<a href="./another/">another page</a>, too! |
|---|
| 55 |
</p> |
|---|
| 56 |
''' + self.footer() |
|---|
| 57 |
index.exposed = True |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
class AnotherPage(Page): |
|---|
| 61 |
title = 'Another Page' |
|---|
| 62 |
|
|---|
| 63 |
def index(self): |
|---|
| 64 |
return self.header() + ''' |
|---|
| 65 |
<p> |
|---|
| 66 |
And this is the amazing second page! |
|---|
| 67 |
</p> |
|---|
| 68 |
''' + self.footer() |
|---|
| 69 |
index.exposed = True |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
cherrypy.tree.mount(HomePage()) |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
if __name__ == '__main__': |
|---|
| 76 |
import os.path |
|---|
| 77 |
thisdir = os.path.dirname(__file__) |
|---|
| 78 |
cherrypy.quickstart(config=os.path.join(thisdir, 'tutorial.conf')) |
|---|