how to append cgi FieldStorage Class instance

Fuzzyman fuzzyman at gmail.com
Fri Jul 29 06:13:28 EDT 2005


praba kar wrote:
> --- Fuzzyman <fuzzyman at gmail.com> wrote:
>
> >
> > praba kar wrote:
> > > Dear All,
> > >     I have doubt in python cgi script. I describe
> > > that doubt with below code
> > > import cgi
> > > form = cgi.FieldStorage()
> > > passwd = form['passwd'].value
> > > print passwd
> > > But Now I want to assign some value to
> > form['passwd']
> > > field value
> > > form['passwd'] = 'surese'
> > > Now If I try to print
> > > print form['passwd'].value. It will raise error.
> > > So kinldy let me how to edit or append instance
> > > of FieldStorage class
> > >
> >
> > A form *isn't* a dictionary, this means that your
> > code :
> >     form['passwd'] = 'surese'
> >
> > is wrong.
> > Turn the form into a dictionary and use that.
>
> How Can I convert a form instance into dictionary
> Kindly let me know.
>

What have you tried ? ;-)

def form_to_dict(form):
    out_dict = {}
    for entry in form:
        out_dict[entry] = form[entry].value
    return out_dict

UNTESTED AND FROM MEMORY
If it doesn't work - try reading the manual to see what I did
wrong.....

> Why are you trying to assign values directly in the
> form ?
>
>       To reduce variable usage in my project.

At the cost of clarity ? Not a worthwhile trade off. Explicity delet
variables you have finished with instead.

form = cgi.FieldStorage()
formdict = form_to_dict(form)
del form


Best Regards,

Fuzzy
http://www.voidspace.org.uk/python

> regards
> Prabahar
>
>
>
>
> __________________________________________________________
> How much free photo storage do you get? Store your friends 'n family snaps for FREE with Yahoo! Photos http://in.photos.yahoo.com




More information about the Python-list mailing list