[mod_python] util.Fieldstorage and form

Graham Dumpleton grahamd at dscpl.com.au
Mon Sep 25 08:04:30 EDT 2006


Please keep followups on mailing list.

On 25/09/2006, at 9:49 PM, sandip wrote:

> Hi,
> Thanks Graham, I am using self contained psp.
>
> my html form is
> <form enctype="multipart/form-data" method="post">
> <input name=x1>
> <input name=x2>
> <input name=UploadFile type=file>
> </form>
>
>
> following is my working code...
> frm = util.FieldStorage(req)
> x1= int(frm.getlist('x1')[0])
> x2 = str(frm.getlist('x2')[0])
> upfile = frm.getlist('UploadFile')[0]
> filename = upfile.filename
> filedata = upfile.value
>
>
> Previously i did this, it didn't worked...means it gave me correct  
> values of x1,x2 byut didn't gave me any attributes of uploaded file..
> x1 = form['x1']
> x2 = form['x2']
> frm = util.FieldStorage(req)
> filename = upfile.filename
> filedata = upfile.value

Which will fail because of a second instance of FieldStorage being  
created.
The first having been created automatically by PSP handler when 'form'
variable is referenced.

What happens when you use:

x1 = form['x1']
x2 = form['x2']
upfile = form['UploadFile']
filename = upfile.filename
filedata = upfile.value

Graham

> Regards,
> Sandip
> ---- Original Message ----- From: "Graham Dumpleton"  
> <grahamd at dscpl.com.au>
> To: "sandip" <sandip.more at gmail.com>
> Cc: <mod_python at modpython.org>
> Sent: Monday, September 25, 2006 5:06 PM
> Subject: Re: [mod_python] util.Fieldstorage and form
>
>
>> Your questions may in part be answered by:
>>
>>   http://issues.apache.org/jira/browse/MODPYTHON-38
>>
>> If FieldStorage object is constructed twice, on the second time   
>> round, it
>> will only be able to provide access to form fields which are part  
>> of the
>> query string of the URL itself and not any field embedded as  
>> multipart
>> form data within the content of the POST request. This was what Jim
>> was getting as except that he didn't mention case where mix of query
>> string and multipart form parameters used.
>>
>> What is not clear from your message is whether you are doing  
>> absolutely
>> everything within a self contained PSP page, or whether you are  
>> actually
>> using a wrapper around it in the form of a custom handler or a  
>> handler
>> such as mod_python.publisher.
>>
>> If you are wrapping PSP stuff on mod_python.publisher functions, you
>> actually need to pass in "form" something like:
>>
>>     settings = {}
>>     ...
>>     settings.update({"form":req.form})
>>     template = psp.PSP(req,filename=path,vars=settings)
>>     template.run()
>>
>> In mod_python 3.3, this will no longer be necessary as PSP has been
>> changed to  use req.form which may be inherited from a custom handler
>> which has already created it and stored it as that named attribute  
>> of  the
>> request object.
>>
>> Can you be clearer about whether you are using mix of query string
>> parameters and multipart form parameters within content of request,
>> and clarify whether you are using PSP only or in conjunction with
>> another handler.
>>
>> Posting an actual small working example of what you are talking about
>> would be good, as then we don't need to guess.
>>
>> Graham
>>
>> On 21/09/2006, at 11:17 PM, sandip wrote:
>>
>>> Thanks Jim. This explains the puzzle.
>>> but form['filefield'] doesn't work for accessing file data.
>>> how to use form to access file data? or Is form being implemented  
>>> partially in mod_python?
>>>
>>> thanks
>>> sandip
>>>
>>>
>>> ----- Original Message ----- From: "Jim Gallacher"   
>>> <jpg at jgassociates.ca>
>>> To: "sandip" <sandip.more at gmail.com>
>>> Cc: <mod_python at modpython.org>
>>> Sent: Thursday, September 21, 2006 6:02 PM
>>> Subject: Re: [mod_python] util.Fieldstorage and form
>>>
>>>
>>>> sandip wrote:
>>>>> Hi all,
>>>>>    Other day i was trying to build upload functionality in psp.  
>>>>> I  am handling form(method: POST) submission in psp.
>>>>> this form contain a hidden element and  a element of type file   
>>>>> for upload.
>>>>> I observed certain things. When I use form['field'] to access   
>>>>> value of hidden form element,and in same code
>>>>> if I use FieldStorage(req) to get the attributes of uploaded   
>>>>> file. I can't access file attributes.
>>>>>
>>>>> but if I use only FieldStorage to access all the elements of   
>>>>> form, I could access it.
>>>>>
>>>>> so why can't I  use both form and FieldStorage in same code?
>>>>
>>>> The form you see in psp is actually a FieldStorage instance  
>>>> which  is automatically created when the psp parse encounters  
>>>> mention of  the form variable.
>>>>
>>>> FieldStorage may only be called once per request, as it  
>>>> consumes  the request body. Wnen it is called a second time,  
>>>> there is  nothing left to read.
>>>>
>>>> The usual solution is to store a reference to the form in your   
>>>> request object, and pass that around to any functions that need it.
>>>>
>>>> <%
>>>>   stuff = form['stuff']
>>>>   req.form = form
>>>> %>
>>>>
>>>> You'll then be able to access the form in code outside of the  
>>>> psp  page.
>>>>
>>>> Jim
>>>
>>> _______________________________________________
>>> Mod_python mailing list
>>> Mod_python at modpython.org
>>> http://mailman.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list