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

Jorey Bump list at joreybump.com
Sun Aug 29 03:10:26 EDT 2004


Tobiah wrote:

> I'm trying to use the FieldStorage class from
> the Publisher Handler... is that possible?

It seems like it's not recommended to use it directly, according to the 
documentation.

> I can read GET vars fine, but not POSTs.

Form data is available in req.form when using publisher (no need to 
import anything to access them). For example, using this form:

   <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.

> Here is a boiled-down example of what I'm
> trying to do:
> 
> -----------------------------------------------
> 
> 
> from mod_python import util
> from mod_python import apache

You shouldn't need to import these with Publisher just to access form data.

> def index(req):
> 
>         req.content_type = "text/html"
>         req.send_http_header()

Not necessary, at least not on my setup.

>         doc = """
>                 <HTML>
>                 <HEAD>
>                         <TITLE>post example</TITLE>
>                 </HEAD>
>                 <BODY>
>                 <FORM ACTION=index.py METHOD=POST NAME=foo>
>                 <INPUT TYPE=text NAME=poster>
>                 <INPUT TYPE=submit NAME=submitter VALUE='GO!'>
>                 <BR>
>         """

That's fine, although I'm not a fan of using index.py for any reason, 
especially if you're just learning mod_python. You might waste time 
dealing with namespace clashes, which wouldn't occur if you gave the 
file a unique name.

>         f = util.FieldStorage(req, 1)
>         for e in f.list:
>                 doc += "Element: %s, Value %s<BR>" % (e.name, e.value)

I'm pretty sure you don't need or want to create an instance of 
FieldStorage in Publisher. You can do something like this instead:

   for key in req.form.keys():
       doc += "Element: %s, Value = %s<BR>" % (key, req.form[key])

>         doc += """
>                 </FORM>
>                 </BODY>
>                 </HTML>
>         """
> 
>         return doc

Try this example, saved as foobar.py (http://localhost/foobar.py/start):

def start(req):
     result = """
<head>
<title>foobar</title>
</head>
<body>
<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>
</body>
</html>
"""
     return result

def somefunction(req):
     x = req.form['foo']
     y = req.form['bar']
     data = "<p>"
     for key in req.form.keys():
        data += """Element: %s, Value = %s<br>
""" % (key, req.form[key])
     data += "</p>"
     result = """<html>
<head>
<title>foobar</title>
</head>
<body>
<p>I would like %s %s. </p>
%s
</body>
</html>
     """ % (x, y, data)
     return result




More information about the Mod_python mailing list