CGI & Python (error)

David Fuess fuess at att.net
Sun Mar 4 13:19:20 EST 2001


Debugging CGI can be a trying task. You might want to enclose the
entire CGI in a try-except structure so you can trap all errors and
return a reasonable result to the server and write an error log for
yourself.

Dave

On Sun, 04 Mar 2001 02:54:46 +0100, Richard van de Stadt
<stadt at cs.utwente.nl> wrote:

>Colin Meeks wrote:
>> 
>> > When accessing  the  script I get the error:
>> >
>> > File "getdate2.py", line 12, in ?
>> >     day=form["day"].value
>> >   File "/usr/lib/python1.5/cgi.py", line 907, in __getitem__
>> >     raise KeyError, key
>> > KeyError: day
>> >
>> > The script works from the command line.
>> >
>> > My problems is:  the script I am trying to run is based on another one.
>> > This other script works. I can't  figure out the what is causing the
>> problem
>> > as the 2 scripts are identical  in every aspect execpt for 1 variable.
>> Both
>> > scripts are run in a page which has all the required parameters passed to
>> it.
>> > Help. Please...
>> If this is what I think it is (sorry it's not immediately clear), but when I
>> try
>> to parse anything from CGI, if nothing is entered in one of the boxes, this
>> seems to be especially for radio buttons, then the variable isn't
>> initialised.
>> 
>> The thing I always do is something like
>> 
>> try:
>>     day=form["day"].value
>> except:
>>     day=1 #or what ever you want the day variable initialised as
>> 
>> I hope this is of some help.
>
>You can also use this:
>
>if form.has_key("day") and form["day"].value) <> "":
>	has_day_action(form["day"].value)
>else:
>	doesnot_have_day_action()
>
>Note that neither of these options work in case the webform field
>is a checkbox list. In such cases you shouldn't use ".value" directly,
>but initially only the field name, like this:
>
>if aForm.has_key("checkbox_list") and aForm["checkbox_list"] <> "":
>	has_checkbox_list_action ()
>
>To get the values of the selected items of a checkbox list (after you
>tested if the fields exist like state above), do this:
>
>checkbox_items =  aForm["checkbox_list"]
>if type(checkbox_items) <> type ([]):
>	checkbox_items = [checkbox_items]
>
>After this, you will have the item(s) of the checkbox list in an array.
>To get the values of the items, walk through the array, and *then* use
>".value", e.g.:
>
>checkbox_item_values = []
>for item in checkbox_items:
>	checkbox_item_values.append(item.value)
>
>Cheers,
>
>Richard.




More information about the Python-list mailing list