[Tutor] Passing data from html to py

Kent Johnson kent37 at tds.net
Thu Mar 24 04:07:57 CET 2005

Vicki Stanfield wrote:
> I have one last question on this particular script. I am using the
> following line to print out the post data:
> for key in form:
>                 print "%s: %s<br>" % (key, form[key].value)

The difficulty is that sometimes you have a single value and sometimes you have a list. If you use 
form.getlist(key) you will always get a list and you can process it like this:

for key in form:
   datalist = form.getlist(key) # always returns a list
   value = ', '.join(datalist)  # turn the list into a string of comma-separated values
   print "%s: %s<br>" % (key, value)


> It works fine except for when the key is to a list object made by the
> following select statement:
> <select id="prod[]" name="prod[]" multiple="multiple" size="4">
>         <option id="MB" name="MB">Motherboards</option>
>         <option id="CPU" name="CPU">Processors</option>
>         <option id="Case" name="Case">Cases</option>
>         <option id="Power" name="Power">Power Supplies</option>
>         <option id="Mem" name="Mem">Memory</option>
>         <option id="HD" name="HD">Hard Drives</option>
>         <option id="Periph" name="Periph">Peripherals</option>
>       </select>
> AttributeError: 'list' object has no attribute 'value'
>       args = ("'list' object has no attribute 'value'",)
> How does one traverse a list object to get the selected data?
> Thanks for any help.
> Vicki
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list