User input question

Eric Brunel eric.brunel at pragmadev.com
Thu May 30 09:06:46 EDT 2002


Ken wrote:
> But how do I set a field on the web browser for people to type in? Don't I
> need a form for people to type in? If so, how do I define the "method",
> "action" etc. from the form header? (eg: "<form name=update
> action=search.cgi method=post>")

Hmmm, we're drifting off-topic here but yes, you do need a form for the 
user to type in. For the "method" and "action" attributes of the form, it's 
a bit complicated:
- "method" controls how the data will be passed from your Web server to 
your script. With Python, if you use the right modules (see below...), this 
attribute isn't that much important, and can often be omitted.
- "action" is the script you'll launch when the user clicks the "submit" 
button. And here's the big problem: this script *must* be on an actual web 
server. Every web server also has a dedicated space for CGI scripts, which 
depends on the server you use. It's a directory, and your script *must* be 
in this directory or one of its sub-directories to work. Therefore, even to 
debug a script, you must have a web server and be able to install your 
script in its CGI area. For the "action" attribute, you should always set 
it to an absolute URL, with or without the server's name (e.g. 
action="http://myserver/cgi-bin/myScript.py", or 
action="/cgi-bin/myScript.py").

I also feel I have to warn you: debugging a CGI script can be *very* 
frustrating... It's quite difficult to set the whole thing to work, and 
when you make errors in your script, you won't get any coherent message 
from the web server. So my advice would be to try to debug your script 
before actually putting it on the server, using another input method than 
HTML forms. Then add form input, and test it on the server.

Concerning the data passed to your script, look at the module named "cgi" 
in the standard Python library. It has a class named FieldStorage that 
allow you to access all fields in the form in a dictionary-like fashion. It 
also guesses the method with which the data was passed, so you don't have 
to bother guessing it or decoding the data: the FieldStorage class does it 
for you. You'll soon find you can't live with this class!

HTH, and good luck.
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list