Deformed Form

MRAB python at mrabarnett.plus.com
Fri Jun 11 15:49:57 EDT 2010


Victor Subervi wrote:
> Ok. Starting over. Here is the script that "generates" the variable 
> "new_passengers_curr_customers":
> 
[snip]
> Now, here's the form that *should* be able to access that variable:
> 
> !/usr/bin/python
> 
> import cgitb; cgitb.enable()
> import cgi
> import sys,os
> sys.path.append(os.getcwd())
> import MySQLdb
> from login import login
> import fpformat
> from New_Passengers_Curr_Customers import New_Passengers_Curr_Customers

This imports from module New_Passengers_Curr_Customers, which calls:

     cgi.FieldStorage()

> from New_Passengers_Addl_Customers import New_Passengers_Addl_Customers
> from New_Passenger import New_Passenger
> 
> form = cgi.FieldStorage()
> 
[snip]

Another call to:

     cgi.FieldStorage()

> 
> *** NOTE THIS: ****
>   new_passengers_curr_customers = 
> New_Passengers_Curr_Customers(customers, flights)
> *** THAT LINE ****
> 
>   if new_passengers_curr_customers > 0:
>     print "<input type='submit' value=' Send ' />"
>   print '</body>\n</html>'
>   cursor.close()
> 
> create_edit_passengers3()
> 
> 
> See that line that I marked right at the end? So here's that script:
> 
> #!/usr/bin/python
> 
[snip]

> import cgitb; cgitb.enable()
> import cgi
> 
> form = cgi.FieldStorage()
> 
> def New_Passengers_Curr_Customers(customers, flights):
> 
> *** RIGHT HERE. SEE THIS LINE? WHY DOES IT WORK HERE AND NOT IN THE 2ND 
> SCRIPT IN THIS HERE EMAIL WHERE IT SHOULD WORK???***
>   new_passengers_curr_customers = 
> int(form.getfirst('new_passengers_curr_customers', 0))
> *** THAT LINE ABOVE. RIGHT ABOVE HERE. OK?? ***
> 
[snip]
> 
> Ok. So I think that was clear now.
> TIA,
> beno
> 
The documentation for cgi.FieldStorage() says:

""To get at submitted form data, it’s best to use the FieldStorage 
class. The other classes defined in this module are provided mostly for 
backward compatibility. Instantiate it exactly once, without arguments. 
This reads the form contents from standard input or the environment 
(depending on the value of various environment variables set according 
to the CGI standard). Since it may consume standard input, it should be 
instantiated only once.""

It might be that the first call to cgi.FieldStorage() is consuming the
data, so the second call sees no data.

You could ensure that there are only 2 types of source file:

1. Script: called by CGI (not sure about the terminology here). It
fetches the values from the form using:

     form = cgi.FieldStorage()

2. Module: imported to provide supporting functions. It never calls
cgi.FieldStorage(). If an imported function needs the form then it's
passed in explicitly.



More information about the Python-list mailing list