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 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>4. Creating a first dynamic website: Hello, you !</title>
<META NAME="description" CONTENT="4. Creating a first dynamic website: Hello, you !">
<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="node7.html">
<LINK REL="previous" HREF="node5.html">
<LINK REL="up" HREF="tut.html">
<LINK REL="next" HREF="node7.html">
<meta name='aesop' content='information'>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node5.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="node7.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="node5.html">3. Creating a first</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="tut.html">CherryPy Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node7.html">5. Templating languages: CHTL</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION006000000000000000000">
4. Creating a first dynamic website: Hello, you !</A>
</H1>
<P>
Edit the file <span class="file">Hello.cpy</span> that we've created in the previous chapter, and change it to:
<P>
<div class="verbatim"><pre>
CherryClass Root:
mask:
def index(self, name="you"):
<html><body>
Hello, <b py-eval="name"></b> !
<form py-attr="request.base" action="" method="get">
Enter your name: <input name=name type=text><br>
<input type=submit value=OK>
</form>
</body></html>
</pre></div>
<P>
Recompile the file and restart the server. Now, refresh the page in your browser. You should see
<div class="verbatim"><pre>
Hello, you
</pre></div>
<P>
followed by a field where you can enter
some text. Enter your name and press the OK button. Now the string has changed to
<P>
<div class="verbatim"><pre>
Hello, "your name"
</pre></div>
<P>
How does it work ?
<P>
This time, the <var>index</var> method has a parameter called <var>name</var>. Just like for any Python method, this
parameter can have a default value (in this case, <var>you</var>). The first time the browser displays the page,
it doesn't pass any <var>name</var> parameter, so <var>name</var> will have its default value in the function.
<P>
When you fill out the text field and hit OK, the browser will request the same page, but this time, <var>name</var> will be
passed as a parameter and it will contain the name you entered.
<P>
Because we used <var>method="get"</var> in the form, the name parameter will be passed using the URL (you can check that the
URL in your browser looks like: <a class="url" href="http://localhost:8000/?name=yourName">http://localhost:8000/?name=yourName</a>).
<P>
Now, edit <span class="file">Hello.cpy</span> and change <var>method="get"</var> to <var>method="post"</var>. Recompile the file, restart
the server and redo the test: it works exactly the same way, except that <var>name=yourName</var> doesn't show up in the URL.
This is because we used a <var>POST</var> method instead of a <var>GET</var> method for the form.
<P>
What we've learned:
<UL>
<LI>Parameters sent by the browser using the URL (GET) or using a POST are passed to the method via regular Python parameters.
It doesn't matter if it's a GET or a POST, or if the parameter is a short string or a large file. It will always be
handled the same way
</LI>
<LI>In a mask method, you can use special tags like <code>py-eval</code> or <code>py-attr</code>. Those tags are part or the CherryPy
templating languages.
</LI>
</UL>
<P>
It's now time to do more intersting things with the templating languages...
<P>
<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node5.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="node7.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="node5.html">3. Creating a first</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="tut.html">CherryPy Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node7.html">5. Templating languages: CHTL</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>
|