File: node9.html

package info (click to toggle)
cherrypy 0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,324 kB
  • ctags: 1,759
  • sloc: python: 14,411; sh: 6,915; perl: 2,472; makefile: 76
file content (148 lines) | stat: -rw-r--r-- 6,155 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>7. Class, instance, method and URL</title>
<META NAME="description" CONTENT="7. Class, instance, method and URL">
<META NAME="keywords" CONTENT="tut">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="tut.css" type='text/css'>
<link rel="first" href="tut.html">
<link rel="contents" href="contents.html" title="Contents">

<LINK REL="next" HREF="node10.html">
<LINK REL="previous" HREF="node8.html">
<LINK REL="up" HREF="tut.html">
<LINK REL="next" HREF="node10.html">
<meta name='aesop' content='information'>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node8.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A HREF="tut.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node10.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy Tutorial</td>
<td><A HREF="node2.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node8.html">6. Views, functions and</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="tut.html">CherryPy Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node10.html">8. Using OOP to</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION009000000000000000000">
7. Class, instance, method and URL</A>
</H1>
So far, we've seen that the URL <a class="url" href="http://localhost:8000">http://localhost:8000</a> triggers a call to the <var>index</var> method of the <var>Root</var>
class. The URL <a class="url" href="http://localhost:8000/viewResult">http://localhost:8000/viewResult</a> triggers a call to the <var>viewResult</var> method of the <var>Root</var>
class.

<UL>
<LI>How does this magic work ?
</LI>
<LI>I know it cannot call the method of a class. It has to call the method of <b>an instance</b> of a class. How do you explain that ?
</LI>
<LI>What if I type the URL <a class="url" href="http://localhost:8000/dir1/dir2/dir3/page">http://localhost:8000/dir1/dir2/dir3/page</a>
</LI>
</UL>

<P>
Let's answer these questions:

<P>
First of all, when you declare a CherryClass in the source file:
<div class="verbatim"><pre>
CherryClass ClassName:
</pre></div>

<P>
CherryPy (or, to be more precise: the executable generated by CherryPy) will automatically create an instance of
this class, called <var>className</var>. This is the name of the class with a lower case first letter. (This is the reason
why CherryClass names should always start with an upper case letter)

<P>
This instance is a global variable and can be acces from anywhere in the program.

<P>
Based on the URL, how does CherryPy know which method of which class instance to call ?

<P>
It uses a simple mechanism:
For the URL <var>host/dir1/dir2/dir3/page</var>, it will call the <var>dir1_dir2_dir3.page()</var> method. So it will expect
your program to have a CherryClass called <var>Dir1_dir2_dir3</var>, which should have a method called <var>page</var>.

<P>
There are 2 special cases:

<UL>
<LI>if there is no first directory (in other words, the URL is in the form: <var>host/page</var>, then it will call the
<var>root.page()</var> method. This means that both URL <var>host/page</var> and <var>host/root/page</var> are perfectly equivalent.
</LI>
<LI>if there is no first directory and no page (in other words, the URL is just the host name), then it will call the
<var>root.index()</var> method.
</LI>
</UL>
<b>Important</b>: In CherryPy-0.9, a new feature was added: if the page <a class="url" href="http://localhost:8000/dir1/dir2/dir3">http://localhost:8000/dir1/dir2/dir3</a> is
requested, CherryPy will convert it first to <var>dir1_dir2.dir3()</var>, so it will expect a <var>dir3</var> method in the
<var>Dir1_dir2</var> CherryClass. But if no such method exists, then it will look for an <var>index</var> method in the
<var>Dir1_dir2_dir3</var> CherryClass. (this would also correspond to <a class="url" href="http://localhost:8000/dir1/dir2/dir3/index">http://localhost:8000/dir1/dir2/dir3/index</a>).

<P>
In the next chapter, we'll see how to use inheritance when we have similar modules inside a website...

<P>

<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node8.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A HREF="tut.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node10.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy Tutorial</td>
<td><A HREF="node2.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node8.html">6. Views, functions and</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="tut.html">CherryPy Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node10.html">8. Using OOP to</A>
<hr>
<span class="release-info">Release 0.10, documentation updated on 19 March 2004.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>