<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>3.1 A Quick Start with the Publisher Handler</title> <META NAME="description" CONTENT="3.1 A Quick Start with the Publisher Handler"> <META NAME="keywords" CONTENT="modpython"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <link rel="STYLESHEET" href="modpython.css"> <link rel="first" href="modpython.html"> <link rel="contents" href="contents.html" title="Contents"> <link rel="index" href="genindex.html" title="Index"> <LINK REL="next" href="tut-overview.html"> <LINK REL="previous" href="tutorial.html"> <LINK REL="up" href="tutorial.html"> <LINK REL="next" href="tut-overview.html"> </head> <body> <DIV CLASS="navigation"> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="tutorial.html"><img src="icons/previous.png" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="tutorial.html"><img src="icons/up.png" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="tut-overview.html"><img src="icons/next.png" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Mod_python Manual</td> <td><A href="contents.html"><img src="icons/contents.png" border="0" height="32" alt="Contents" width="32"></A></td> <td><img src="icons/blank.png" border="0" height="32" alt="" width="32"></td> <td><A href="genindex.html"><img src="icons/index.png" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A> <b class="navlabel">Up:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A> <b class="navlabel">Next:</b> <a class="sectref" href="tut-overview.html">3.2 Quick Overview of</A> <br><hr> </DIV> <!--End of Navigation Panel--> <H1><A NAME="SECTION005100000000000000000"> </A> <BR> 3.1 A Quick Start with the Publisher Handler </H1> <P> This section provides a quick overview of the Publisher handler for those who would like to get started without getting into too much detail. A more thorough explanation of how mod_python handlers work and what a handler actually is follows on in the later sections of the tutorial. <P> The <code>publisher</code> handler is provided as one of the standard mod_python handlers. To get the publisher handler working, you will need the following lines in your config: <P> <dl><dd><pre class="verbatim"> AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On </pre></dl> <P> The following example will demonstrate a simple feedback form. The form will ask for the name, e-mail address and a comment and construct an e-mail to the webmaster using the information submitted by the user. This simple application consists of two files: <span class="file">form.html</span> - the form to collect the data, and <span class="file">form.py</span> - the target of the form's action. <P> Here is the html for the form: <P> <dl><dd><pre class="verbatim"> <html> Please provide feedback below: <p> <form action="form.py/email" method="POST"> Name: <input type="text" name="name"><br> Email: <input type="text" name="email"><br> Comment: <textarea name="comment" rows=4 cols=20></textarea><br> <input type="submit"> </form> </html> </pre></dl> <P> Note the <code>action</code> element of the <code><form></code> tag points to <code>form.py/email</code>. We are going to create a file called <span class="file">form.py</span>, like this: <P> <dl><dd><pre class="verbatim"> import smtplib WEBMASTER = "webmaster" # webmaster e-mail SMTP_SERVER = "localhost" # your SMTP server def email(req, name, email, comment): # make sure the user provided all the parameters if not (name and email and comment): return "A required parameter is missing, \ please go back and correct the error" # create the message text msg = """\ From: %s Subject: feedback To: %s I have the following comment: %s Thank You, %s """ % (email, WEBMASTER, comment, name) # send it out conn = smtplib.SMTP(SMTP_SERVER) conn.sendmail(email, [WEBMASTER], msg) conn.quit() # provide feedback to the user s = """\ <html> Dear %s,<br> Thank You for your kind comments, we will get back to you shortly. </html>""" % name return s </pre></dl> <P> When the user clicks the Submit button, the publisher handler will load the <tt class="function">email</tt> function in the <tt class="module">form</tt> module, passing it the form fields as keyword arguments. It will also pass the request object as <code>req</code>. <P> Note that you do not have to have <code>req</code> as one of the arguments if you do not need it. The publisher handler is smart enough to pass your function only those arguments that it will accept. <P> The data is sent back to the browser via the return value of the function. <P> Even though the Publisher handler simplifies mod_python programming a great deal, all the power of mod_python is still available to this program, since it has access to the request object. You can do all the same things you can do with a ``native'' mod_python handler, e.g. set custom headers via <code>req.headers_out</code>, return errors by raising <tt class="exception">apache.SERVER_ERROR</tt> exceptions, write or read directly to and from the client via <tt class="method">req.write()</tt> and <tt class="method">req.read()</tt>, etc. <P> Read Section <A href="hand-pub.html#hand-pub">6.1</A> <em class="citetitle"><a href="hand-pub.html" title="Publisher Handler" >Publisher Handler</a></em> for more information on the publisher handler. <P> <DIV CLASS="navigation"> <p><hr> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="tutorial.html"><img src="icons/previous.png" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="tutorial.html"><img src="icons/up.png" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="tut-overview.html"><img src="icons/next.png" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Mod_python Manual</td> <td><A href="contents.html"><img src="icons/contents.png" border="0" height="32" alt="Contents" width="32"></A></td> <td><img src="icons/blank.png" border="0" height="32" alt="" width="32"></td> <td><A href="genindex.html"><img src="icons/index.png" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A> <b class="navlabel">Up:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A> <b class="navlabel">Next:</b> <a class="sectref" href="tut-overview.html">3.2 Quick Overview of</A> <hr> <span class="release-info">Release 3.1.0a, documentation updated on August 26, 2003.</span> </DIV> <!--End of Navigation Panel--> </BODY> </HTML>