I'm going crazy using cgi module. HELP PLEASE!!!!

Stefan Schwarzer s.schwarzer at ndh.net
Sun Apr 7 16:17:04 EDT 2002


Hello Enrico

Enrico Morelli wrote:
> I'm going crazy using cgi module.
> Follow a main routine of a program that receives for each day of the
> month, six fields from a form.
> 
> The first  'print ar'  inside of  'for' cycle, works fine, display the
> correct ar contents.
> The second one (outside of 'for' cycle) doesn't display nothing.

You probably mean "doesn't display anything" or "does display nothing"?

> In fact,  if I try to write the array on a file, the file will be
> empty.
> 
> I use python2.2 on Linux RH7.2  system.
> 
> Where I wrong???
>
> I use cgi modules for more and more other forms without problems.

I didn't investigate your code in detail but it may be that there's an
defect in it which causes an exception. You may use

import sys
sys.stderr = sys.stdout

at the top of your module to receive a traceback in the browser window
or catch the exception with

try:
    ...
except:
    # do something with sys.exc_info()
 
> Thanks
> 
> #----------------------------------------#
> # Main Program
> def main():
> 
>         months={1 : 'Gen',
>                 2 : 'Feb',
>                 3 : 'Mar',
>                 4 : 'Apr',
>                 5 : 'Mag',
>                 6 : 'Giu',
>                 7 : 'Lug',
>                 8 : 'Ago',
>                 9 : 'Set',
>                 10 : 'Ott',
>                 11 : 'Nov',
>                 12 : 'Dic'}
> 
>         MONTH_NUMBER="%m"
>         YEAR_FORMAT="%Y"
>         currenttime = time.time()
>         nmonth=time.strftime(MONTH_NUMBER,
> time.localtime(currenttime))
>         year=time.strftime(YEAR_FORMAT, time.localtime(currenttime))
> 
>         print "Content-Type: text/plain\n\n"
> 
>         form = cgi.FieldStorage()
> 
>         im=int(form['month'].value)
>         month=months[int(nmonth)+im]
>         nmonth=int(nmonth)+im
> 
>         planningsavefile=planningsave + '-' + month
>         dmonth=calendar.monthcalendar(int(year), nmonth)
> 
>         for dd in range(len(dmonth)):
>                 for mm in range(len(dmonth[dd])):
>                         if dmonth[dd][mm] == 0:
>                                 continue
>                         ar.append([dmonth[dd][mm],'','','','','',''])
> 
>         print ar # works fine
>         for name in form.keys():
> 
>                 if name=='submit':
>                         continue
>                  # name has the values like: 2-7 or 2-8 or 1-7b or
> 4-4, etc.
>                 d=string.split(name, '-')[0]
>                 s=string.split(name, '-')[1]

Maybe use

d, s = string.split(name, '-')[:2]

> 
>                 d=int(d)-1
>                 print name, d,s # Works fine

>                 if s=='7b':
>                         ar[d][2]=form[name].value
>                 elif s=='8':
>                         ar[d][1]=form[name].value
>                 elif s=='7':
>                         ar[d][3]=form[name].value
>                 elif s=='6':
>                         ar[d][4]=form[name].value
>                 elif s=='5':
>                         ar[d][5]=form[name].value
>                 elif s=='4':
>                         ar[d][6]=form[name].value

What about setting up a dictionary at module level ...

# I would use a better name if I knew what's this for :-)
s_to_index = {'7b': 2, '8': 1, '7': 3, ...}

and use

if s_to_index.has_key(s):
    ar[d][ s_to_index[s] ] = form[name].value

instead of if ... elif ... elif ...

>                 print ar # works fine
> 
>         print ar # doesn't display nothing

Stefan



More information about the Python-list mailing list