[mod_python] Can't get post vars using Publisher Handler

Tom thomas at pixtur.de
Fri Oct 15 16:21:55 EDT 2004

Thanks for this tip!

This example should definately be added to the tutorial section of the 
mod_python-docu, because:

1. Parsing form-variables is the first thing a newby to mod_python wants 
to do

2. No information about req.form() available under:

I spent the last few hours wondering about req.read() (which returned no 
string, even if req.headers_in["content-length"] showed a length 
different from zero.

        len = int(req.headers_in["content-length"])
        req.write("%s" % len);               # printed something about 40
        req.write("%s" % req.read());  #printed nothing

        req.write("%s" % req.form.keys());   # worked fine

    python newby

>   <form name="test" action="somefunction" method="POST">
>     <input type="hidden" name="foo" value="1">
>     <input type="hidden" name="bar" value="milkyway">
>     <input type="submit" name="submit" value="Try me!">
>   </form>
> the variables will be available in the function like this:
>   def somefunction(req):
>       x = req.form['foo']
>       y = req.form['bar']
>       result = """<html>
>   <head>
>   <title>foobar</title>
>   </head>
>   <body>
>   <p>I would like %s %s. </p>
>   </body>
>   </html>
>       """ % (x, y)
>       return result
> Note that the values of multiple form elements of the same name will 
> be returned as a list, so process accordingly.

More information about the Mod_python mailing list