The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Reading values from GET using cgi
Discuss Reading values from GET using cgi in the Python Programming forum on Dev Shed. Reading values from GET using cgi Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 9th, 2004, 05:19 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 71
 
Time spent in forums: 1 h 20 m 49 sec
Reputation Power: 10
|
|
|
Reading values from GET using cgi
HI.
I have a bbt of a problem and I would appreciated your help.
Usually to read values, I use
form = cgi.FieldStorage()
and then treat the form variable like a dictionary type
but....
how can you read the values if there are multiple keys, for example
if we write in the HTML
<select Name="lstRsrcs" SIZE='4' MULTIPLE>
<option VALUE="Rsrc1">Rsrc1Desc</option>
<option VALUE="Rsrc2">Rsrc2Desc</option>
<option VALUE="Rsrc3">Rsrc3Desc</option>
</select>
and we select more then one option, the http will look something like:
url ?lstRsrcs=Rsrc1&lstRsrcs=Rsrc2&lstRsrcs=Rsrc3
how can I read that?! or iterate through that?
Thanks a lot
Roy
|

March 10th, 2004, 03:00 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
If you are using version 2.2 or later you can use the getlist method. From the docs:
Quote: A more convenient approach is to use the methods getfirst() and getlist() provided by this higher level interface.
getfirst( name[, default])
This method always returns only one value associated with form field name. The method returns only the first value in case that more values were posted under such name. Please note that the order in which the values are received may vary from browser to browser and should not be counted on. If no such form field or value exists then the method returns the value specified by the optional parameter default. This parameter defaults to None if not specified.
getlist( name)
This method always returns a list of values associated with form field name. The method returns an empty list if no such form field or value exists for name. It returns a list consisting of one item if only one such value exists.
Using these methods you can write nice compact code:
Code:
import cgi
form = cgi.FieldStorage()
user = form.getfirst("user", "").toupper() # This way it's safe.
for item in form.getlist("item"):
do_something(item)
|
For versions prior to 2.2, the FieldStorage.getvalue(name) method returns a string if there is only one item and a list if there is more than one, so you have to check explicitly. From the docs again:
Code:
item = form.getvalue("item")
if isinstance(item, list):
# The user is requesting more than one item.
else:
# The user is requesting only one item.
Regards,
Dave - The Developers' Coach
|

March 10th, 2004, 02:05 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
I recently had to write a form parser as part of my net module, as part of this it had to accomodate multiple fields with the same value. Anyway i've attached the file; as an example of parsing and use values from GET/QUERY_STRING.
Have fun,
Mark.
__________________
programming language development: www.netytan.com – Hula
|

March 11th, 2004, 02:00 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 71
 
Time spent in forums: 1 h 20 m 49 sec
Reputation Power: 10
|
|
|
Thanks Dave
Quote: | Originally Posted by DevCoach If you are using version 2.2 or later you can use the getlist method. From the docs:
For versions prior to 2.2, the FieldStorage.getvalue(name) method returns a string if there is only one item and a list if there is more than one, so you have to check explicitly. From the docs again:
Code:
item = form.getvalue("item")
if isinstance(item, list):
# The user is requesting more than one item.
else:
# The user is requesting only one item.
Regards,
Dave - The Developers' Coach |
This is exactly what iv'e done and it works beautiful
thank you for the very good explanation and examples
Roy
|

March 11th, 2004, 02:04 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 71
 
Time spent in forums: 1 h 20 m 49 sec
Reputation Power: 10
|
|
|
Thanks Mark
Quote: | Originally Posted by netytan I recently had to write a form parser as part of my net module, as part of this it had to accomodate multiple fields with the same value. Anyway i've attached the file; as an example of parsing and use values from GET/QUERY_STRING.
Have fun,
Mark. |
I solved the problem by using form.GetList method that Dave suggested.
I will read your module throughly, and I'm sure that I can learn a lot from that.
Thanks a lot
Roy
|

March 11th, 2004, 03:04 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
No problem at all roypy, i didnt expect that you would be wrting you're own form parser to solve a simple problem like this but as an example of how a form parser might work it works very well
Take care,
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|