1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
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">
19
<DIV CLASS="navigation">
20
<table align="center" width="100%" cellpadding="0" cellspacing="2">
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>
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>
47
<!--End of Navigation Panel-->
49
<H1><A NAME="SECTION005100000000000000000"> </A>
51
3.1 A Quick Start with the Publisher Handler
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
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:
67
<dl><dd><pre class="verbatim">
68
AddHandler mod_python .py
69
PythonHandler mod_python.publisher
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.
82
Here is the html for the form:
85
<dl><dd><pre class="verbatim">
87
Please provide feedback below:
89
<form action="form.py/email" method="POST">
91
Name: <input type="text" name="name"><br>
92
Email: <input type="text" name="email"><br>
93
Comment: <textarea name="comment" rows=4 cols=20></textarea><br>
94
<input type="submit">
101
Note the <code>action</code> element of the <code><form></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:
106
<dl><dd><pre class="verbatim">
109
WEBMASTER = "webmaster" # webmaster e-mail
110
SMTP_SERVER = "localhost" # your SMTP server
112
def email(req, name, email, comment):
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"
119
# create the message text
125
I have the following comment:
133
""" % (email, WEBMASTER, comment, name)
136
conn = smtplib.SMTP(SMTP_SERVER)
137
conn.sendmail(email, [WEBMASTER], msg)
140
# provide feedback to the user
145
Thank You for your kind comments, we
146
will get back to you shortly.
148
</html>""" % name
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>.
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.
165
The data is sent back to the browser via the return value of the
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>,
179
Read Section <A href="hand-pub.html#hand-pub">6.1</A> <em class="citetitle"><a
181
title="Publisher Handler"
182
>Publisher Handler</a></em>
183
for more information on the publisher handler.
187
<DIV CLASS="navigation">
189
<table align="center" width="100%" cellpadding="0" cellspacing="2">
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>
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>
215
<span class="release-info">Release 3.1.3, documentation updated on February 17, 2004.</span>
217
<!--End of Navigation Panel-->