[mod_python] Form data handling (newbie question)

Lee E. Brown Administrator at leebrown.org
Thu Nov 25 22:18:29 EST 2004


In mod_python, form data is returned as a mapping type (dictionary) instead of a sequence type (tuple or list.)

A very handy way to get the form data is to import the util module from mod python:

    from mod_python import util

and then within your handler instantiate an object of the FieldStorage Class:

    def myhandler(req):
        formdata = util.FieldStorage(req)

You can then get any of the form's values by name.  Say your form is...

    <input name="age"...
    <input name="gender"...
    <input name="shoe_size"...

...you can then get all your form data from the formdata object:

    if formdata['gender'] == 'male':
        oddness_coefficient = formdata['age'] + formdata['shoe_size']

In my opinion, accessing your form data by name instead of by sequence offsets makes your code a LOT easier to read.

I hope this is of some help.

Best Regards,
Lee E. Brown

----- Original Message ----- 
From: "Lukas Linhart" <almad at include.cz>
To: <mod_python at modpython.org>
Sent: Thursday, November 25, 2004 7:33 PM
Subject: [mod_python] Form data handling (newbie question)

I'm beginning with mod_python and using mod_python.publisher. 

I've programmed in php before and I'm used to handle large forms in way of 
arrays (tuples): 

<input name="in[0]" value="blabla" />
<input name="in[1] value="blabla" />

I generate form like this in php and then on next page handle it in very 
comfortable way. 

Well, I'm asking if there is any way to handle forms in mod_python like this? 
I expected to work it like this: 

def form_handler(req, *everyURIParams)

but this don't work, as like this:

def form_handler(req, in)


def form_handler(req.*in)

I understand why it won't work (problably) and asking if there is any other 
way / workaround / more standard way. 

I'm very thankful for your help. 

Lukas "Almad" Linhart
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20041125/ac75a066/attachment.html

More information about the Mod_python mailing list