[Tutor] converting CGI's FieldStorage to dictionary?

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 11 Apr 2001 17:10:54 -0700 (PDT)


On Mon, 9 Apr 2001, Lance E Sloan wrote:

> The problem I'm running into is that DocumentTemplate uses
> dictionaries as arguments to hold template values, but cgi's
> FieldStorage is not quite a dictionary.  So, this doesn't work:
> 
> 	import cgi
> 	import DocumentTemplate
> 
> 	form = cgi.FieldStorage()
> 	tmpl = DocumentTemplate.HTMLFile('path/to/file')
> 	print tmpl(mapping = form)
> 
> How can I write a function that takes my FieldStorage form as an
> argument and returns a dictionary of field names to field values?


Let's write a function that takes a FieldStorage, and returns back an
equivalent dictionary:

###
def fieldStorageToDictionary(fs):
    mydict = {}
    for k, v in fs.items():
        mydict[k] = v.value
    return mydict
###

To tell the truth, I haven't tested this code yet, but hopefully this is
close enough that it'll help you write that function.  Good luck!