#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    71
    Rep Power
    11

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    If you are using version 2.2 or later you can use the getlist method. From the docs:

    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
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    Attached Files
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    71
    Rep Power
    11

    Thanks Dave


    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    71
    Rep Power
    11

    Thanks Mark


    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
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo