March 9th, 2004, 05:19 PM
Reading values from GET using cgi
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
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>
and we select more then one option, the http will look something like:
how can I read that?! or iterate through that?
Thanks a lot
March 10th, 2004, 03:00 AM
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:
item = form.getvalue("item")
if isinstance(item, list):
# The user is requesting more than one item.
# The user is requesting only one item.
Dave - The Developers' Coach
March 10th, 2004, 02:05 PM
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.
March 11th, 2004, 02:00 PM
This is exactly what iv'e done and it works beautiful
Originally Posted by DevCoach
thank you for the very good explanation and examples
March 11th, 2004, 02:04 PM
I solved the problem by using form.GetList method that Dave suggested.
Originally Posted by netytan
I will read your module throughly, and I'm sure that I can learn a lot from that.
Thanks a lot
March 11th, 2004, 03:04 PM
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