[mod_python] example of mod_python publisher handling html forms and cgi variables

Dave Britton dave at davebritton.com
Sat Feb 4 22:24:28 EST 2006

Thanks. I'll forward this to the list so that others can find it in the
archives if it's helpful.
----- Original Message ----- 
From: "Glenn Hammonds" <rghammonds at gmail.com>
To: "Dave Britton" <dave at davebritton.com>
Sent: Saturday, February 04, 2006 8:55 PM
Subject: Re: I like how you use mod_python


Thanks so much.  I did take a look, and that bit of code is just
extraordinarily helpful for someone just getting going.  I really
appreciate it! I wish that or something like it was in the FAQ or even
in the documentation as an example.



On 2/4/06, Dave Britton <dave at davebritton.com> wrote:
> Sure -
> 1. be sure you have mod_python publisher installed - something like this
> should be in httpd.conf:
>   <Directory /var/www/dave/python/>
>      AddHandler python-program .py
>      PythonHandler mod_python.publisher
>      PythonDebug On
>    </Directory>
> 2. Then write a python module that has a function named index() in it, and
> put it into the directory specified in the conf file. when you browse to
> module name (not including the .py extension) publisher will run the index
> function as if it were the index.html default file in a regular html
> directory.
> 3. I put an example program at http://www.davebritton.com/python/myexample
> for you to see.
> Let me know if you have any questions.
> -Dave
==================== source code for myexample.py ==========
# myexample.py
# this produces and processes a simple cgi form
def index(req, var1='', var2='off', var3='', hiddenvar='0'):
    # note - req is the Request object that holds the cgi interface data
    # the var1-3s are the variables that my form will be passing in to be
    # hiddenvar is an example of using an html form hidden variable, useful
for pseudo-session data
    # start the string to paint the form with html
    # first we want to know if var2 is checked or not (it is a checkbox -
see below)
    if var2=='on':
<h3>Hello there!</h3>
Var 1 is %s, var2 is %s and var3 is %s
<p>If you'd like to change those values, enter new ones into this form:<br>
<form action = "myexample" method="post">
Enter var1: <input type="text" name = "var1" value =" %s"><br>
Enter var2: <input type="checkbox" name = "var2" %s><br>
Enter var3: <textarea name = "var3">%s</textarea><br>
    """ %(var1,var2,var3,var1,checked,var3)
    # now let's increment our hidden value counter and tell the user what it
    # cgi vars are always strings, so you have to convert them to numbers
and back
    hiddenvar = str(int(hiddenvar)+1)
    # add some more html to the output string to finish the form
    htmlout += """
<input type = "hidden" name = "hiddenvar" value = "%s">
<input type = "submit" value = "Click here to set the values">
This is the count of how many times you have been shown this form: %s
<p><a href="myexample.py-source">Click to view or right-click and save-as to
download the source code</a>
    """%(hiddenvar, hiddenvar)
    # that's all we need to do, so just return the string:
    return htmlout

> ============================================
> ----- Original Message -----
> From: "Glenn Hammonds" <rghammonds at gmail.com>
> To: <dave at davebritton.com>
> Sent: Saturday, February 04, 2006 3:09 PM
> Subject: I like how you use mod_python
> Dave,
> I read with interest your post on the mod_python list replying to "how
> you use python".
> I'm just starting up with mod_python, moving existing too slow code
> from cgi, and your 1-8 explanation of how you use it made the most
> sense to me of any I've seen anywhere.
> I am currently conceptually stuck on points (2), collecting incoming
> cgi variables and (8) waiting for the user click.  I can't seem to get
> how to do that in mod_python.
> Would you be willing to share a skeleton version that explains this?
> I know it must be dead simple, but then ... so am I from time to time.
> Thanks in advance,
> Glenn

More information about the Mod_python mailing list