CGI multiple select?

Denis S. Otkidach ods at fep.ru
Tue Apr 9 06:18:11 EDT 2002


On Mon, 8 Apr 2002, John wrote:

J> In my html form, I have 'multiple select'
J>
J> <select name=spred multiple>
J> <option..>
J> <option..>
J>
J>
J> This form invokes CGI routine, where I try to read selected
J> entries from
J> form["spred"]
J>
J> My dilemma is here:
J>
J> When only one entry is selected:
J> form["spred"].value (will be the object selected)
J>
J> When multiple entries were selected:
J> for o in form["spred"]:
J>   o.value (--> each of these will be the object selected)
J>
J> Thus, I need to handle according to situation.. I though my
J> previous
J> question on 'isList' can solve this matter, but somehow, all
J> solution
J> suggested (isSequenceType, type, ..) cannot distinguish these
J> two cases.

If you are using apache and set LimitRequestBody to reasonable
small value (for security reasons), the most safe way is to use
getlist() of FieldStorage _always_.  Then you keep yourself form
doing many "if ... else" or "try ... except".

If you need the list of options -- just call it and you always
recieve a list (may be empty).  If there is the only field:

field = form.getlist('field')
if field:
    # use field[0]
else:
    # field is empty

Or just this way (put any default value you want in list below):

field = (form.getlist('field')+[''])[0]






More information about the Python-list mailing list