~ubuntu-branches/ubuntu/jaunty/libapache2-mod-python/jaunty

« back to all changes in this revision

Viewing changes to doc-html/tut-pub.html

  • Committer: Bazaar Package Importer
  • Author(s): Thom May
  • Date: 2004-09-06 20:27:57 UTC
  • Revision ID: james.westby@ubuntu.com-20040906202757-yzpyu1bcabgpjtiu
Tags: upstream-3.1.3
ImportĀ upstreamĀ versionĀ 3.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 
2
<html>
 
3
<head>
 
4
<title>3.1 A Quick Start with the Publisher Handler</title>
 
5
<META NAME="description" CONTENT="3.1 A Quick Start with the Publisher Handler">
 
6
<META NAME="keywords" CONTENT="modpython">
 
7
<META NAME="resource-type" CONTENT="document">
 
8
<META NAME="distribution" CONTENT="global">
 
9
<link rel="STYLESHEET" href="modpython.css">
 
10
<link rel="first" href="modpython.html">
 
11
<link rel="contents" href="contents.html" title="Contents">
 
12
<link rel="index" href="genindex.html" title="Index">
 
13
<LINK REL="next" href="tut-overview.html">
 
14
<LINK REL="previous" href="tutorial.html">
 
15
<LINK REL="up" href="tutorial.html">
 
16
<LINK REL="next" href="tut-overview.html">
 
17
</head>
 
18
<body>
 
19
<DIV CLASS="navigation">
 
20
<table align="center" width="100%" cellpadding="0" cellspacing="2">
 
21
<tr>
 
22
<td><A href="tutorial.html"><img src="icons/previous.gif"
 
23
  border="0" height="32"
 
24
  alt="Previous Page" width="32"></A></td>
 
25
<td><A href="tutorial.html"><img src="icons/up.gif"
 
26
  border="0" height="32"
 
27
  alt="Up One Level" width="32"></A></td>
 
28
<td><A href="tut-overview.html"><img src="icons/next.gif"
 
29
  border="0" height="32"
 
30
  alt="Next Page" width="32"></A></td>
 
31
<td align="center" width="100%">Mod_python Manual</td>
 
32
<td><A href="contents.html"><img src="icons/contents.gif"
 
33
  border="0" height="32"
 
34
  alt="Contents" width="32"></A></td>
 
35
<td><img src="icons/blank.gif"
 
36
  border="0" height="32"
 
37
  alt="" width="32"></td>
 
38
<td><A href="genindex.html"><img src="icons/index.gif"
 
39
  border="0" height="32"
 
40
  alt="Index" width="32"></A></td>
 
41
</tr></table>
 
42
<b class="navlabel">Previous:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A>
 
43
<b class="navlabel">Up:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A>
 
44
<b class="navlabel">Next:</b> <a class="sectref" href="tut-overview.html">3.2 Quick Overview of</A>
 
45
<br><hr>
 
46
</DIV>
 
47
<!--End of Navigation Panel-->
 
48
 
 
49
<H1><A NAME="SECTION005100000000000000000">&nbsp;</A>
 
50
<BR>
 
51
3.1 A Quick Start with the Publisher Handler
 
52
</H1>
 
53
 
 
54
<P>
 
55
This section provides a quick overview of the Publisher handler for
 
56
those who would like to get started without getting into too much
 
57
detail. A more thorough explanation of how mod_python handlers work
 
58
and what a handler actually is follows on in the later sections of the
 
59
tutorial.
 
60
 
 
61
<P>
 
62
The <code>publisher</code> handler is provided as one of the standard
 
63
mod_python handlers. To get the publisher handler working, you will
 
64
need the following lines in your config:
 
65
 
 
66
<P>
 
67
<dl><dd><pre class="verbatim">
 
68
  AddHandler mod_python .py
 
69
  PythonHandler mod_python.publisher
 
70
  PythonDebug On
 
71
</pre></dl>
 
72
 
 
73
<P>
 
74
The following example will demonstrate a simple feedback form. The
 
75
form will ask for the name, e-mail address and a comment and construct
 
76
an e-mail to the webmaster using the information submitted by the
 
77
user. This simple application consists of two files:
 
78
<span class="file">form.html</span> - the form to collect the data, and
 
79
<span class="file">form.py</span> - the target of the form's action.
 
80
 
 
81
<P>
 
82
Here is the html for the form:
 
83
 
 
84
<P>
 
85
<dl><dd><pre class="verbatim">
 
86
  &lt;html&gt;
 
87
      Please provide feedback below:
 
88
  &lt;p&gt;                           
 
89
  &lt;form action="form.py/email" method="POST"&gt;
 
90
 
 
91
      Name:    &lt;input type="text" name="name"&gt;&lt;br&gt;
 
92
      Email:   &lt;input type="text" name="email"&gt;&lt;br&gt;
 
93
      Comment: &lt;textarea name="comment" rows=4 cols=20&gt;&lt;/textarea&gt;&lt;br&gt;
 
94
      &lt;input type="submit"&gt;
 
95
 
 
96
  &lt;/form&gt;
 
97
  &lt;/html&gt;
 
98
</pre></dl>
 
99
 
 
100
<P>
 
101
Note the <code>action</code> element of the <code>&lt;form&gt;</code> tag points to
 
102
<code>form.py/email</code>. We are going to create a file called
 
103
<span class="file">form.py</span>, like this:
 
104
 
 
105
<P>
 
106
<dl><dd><pre class="verbatim">
 
107
import smtplib
 
108
 
 
109
WEBMASTER = "webmaster"   # webmaster e-mail
 
110
SMTP_SERVER = "localhost" # your SMTP server
 
111
 
 
112
def email(req, name, email, comment):
 
113
 
 
114
    # make sure the user provided all the parameters
 
115
    if not (name and email and comment):
 
116
        return "A required parameter is missing, \
 
117
               please go back and correct the error"
 
118
 
 
119
    # create the message text
 
120
    msg = """\
 
121
From: %s                                                                                                                                           
 
122
Subject: feedback
 
123
To: %s
 
124
 
 
125
I have the following comment:
 
126
 
 
127
%s
 
128
 
 
129
Thank You,
 
130
 
 
131
%s
 
132
 
 
133
""" % (email, WEBMASTER, comment, name)
 
134
 
 
135
    # send it out
 
136
    conn = smtplib.SMTP(SMTP_SERVER)
 
137
    conn.sendmail(email, [WEBMASTER], msg)
 
138
    conn.quit()
 
139
 
 
140
    # provide feedback to the user
 
141
    s = """\
 
142
&lt;html&gt;
 
143
 
 
144
Dear %s,&lt;br&gt;                                                                                                                                       
 
145
Thank You for your kind comments, we
 
146
will get back to you shortly.
 
147
 
 
148
&lt;/html&gt;""" % name
 
149
 
 
150
    return s
 
151
</pre></dl>
 
152
 
 
153
<P>
 
154
When the user clicks the Submit button, the publisher handler will
 
155
load the <tt class="function">email</tt> function in the <tt class="module">form</tt> module,
 
156
passing it the form fields as keyword arguments. It will also pass the
 
157
request object as <code>req</code>.
 
158
 
 
159
<P>
 
160
Note that you do not have to have <code>req</code> as one of the arguments
 
161
if you do not need it. The publisher handler is smart enough to pass
 
162
your function only those arguments that it will accept.
 
163
 
 
164
<P>
 
165
The data is sent back to the browser via the return value of the
 
166
function.
 
167
 
 
168
<P>
 
169
Even though the Publisher handler simplifies mod_python programming a
 
170
great deal, all the power of mod_python is still available to this
 
171
program, since it has access to the request object. You can do all the
 
172
same things you can do with a ``native'' mod_python handler, e.g. set
 
173
custom headers via <code>req.headers_out</code>, return errors by raising
 
174
<tt class="exception">apache.SERVER_ERROR</tt> exceptions, write or read directly to
 
175
and from the client via <tt class="method">req.write()</tt> and <tt class="method">req.read()</tt>,
 
176
etc.
 
177
 
 
178
<P>
 
179
Read Section <A href="hand-pub.html#hand-pub">6.1</A> <em class="citetitle"><a
 
180
 href="hand-pub.html"
 
181
 title="Publisher Handler"
 
182
 >Publisher Handler</a></em>
 
183
for more information on the publisher handler. 
 
184
 
 
185
<P>
 
186
 
 
187
<DIV CLASS="navigation">
 
188
<p><hr>
 
189
<table align="center" width="100%" cellpadding="0" cellspacing="2">
 
190
<tr>
 
191
<td><A href="tutorial.html"><img src="icons/previous.gif"
 
192
  border="0" height="32"
 
193
  alt="Previous Page" width="32"></A></td>
 
194
<td><A href="tutorial.html"><img src="icons/up.gif"
 
195
  border="0" height="32"
 
196
  alt="Up One Level" width="32"></A></td>
 
197
<td><A href="tut-overview.html"><img src="icons/next.gif"
 
198
  border="0" height="32"
 
199
  alt="Next Page" width="32"></A></td>
 
200
<td align="center" width="100%">Mod_python Manual</td>
 
201
<td><A href="contents.html"><img src="icons/contents.gif"
 
202
  border="0" height="32"
 
203
  alt="Contents" width="32"></A></td>
 
204
<td><img src="icons/blank.gif"
 
205
  border="0" height="32"
 
206
  alt="" width="32"></td>
 
207
<td><A href="genindex.html"><img src="icons/index.gif"
 
208
  border="0" height="32"
 
209
  alt="Index" width="32"></A></td>
 
210
</tr></table>
 
211
<b class="navlabel">Previous:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A>
 
212
<b class="navlabel">Up:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A>
 
213
<b class="navlabel">Next:</b> <a class="sectref" href="tut-overview.html">3.2 Quick Overview of</A>
 
214
<hr>
 
215
<span class="release-info">Release 3.1.3, documentation updated on February 17, 2004.</span>
 
216
</DIV>
 
217
<!--End of Navigation Panel-->
 
218
 
 
219
</BODY>
 
220
</HTML>