Some newbie cgi form questions...

G

googleboy

Hi there.

I am having a bit of a play with teh cgi module, trying to figure out
how to process a web form.

I have come a little ways from reading up variou sexamples and tutes
that I've found on the net, but they all leave me with a couple of
questions:

My code below has an attempt at validation that doesn't seem to do
anything. I am expecting that it will report an error if all the
fields haven't been filled in. Instead, it seems to just ignore an
empty field and carry on.

The second for loop is an attempt at trying to print all the values
that were entered on the for without presenting the hidden values. I'd
really like to do this, but I can't seem to figure out how to make a
special case for hidden form values, nor can I find an example of how
to do it in any of the many python cgi pages I've visited so far. If I
removed the "if not type=hidden" and fix the indentation this works
pretty much as I expect.


TIA

Googleboy



print 'Content-type: text/html\n\n'
print htmlheader

for key in form.keys():
if not form.has_key(key):
print "<h1> ERROR! <h1><BR>"
print "Please go back and fill in all fields. All fields are
required.<br>"
break

print "You entered the following:<br> "
for key in form.keys():
if not type="hidden":
print form[key].value + '<br><br>'
print '<br>' + timestamp
print '<br><br>'
print '</body></html>'
 
A

Atila Olah

for key in form.keys():
if not form.has_key(key): # it will always be True

try this:

for key in form.keys():
if not form.keys()[key]: # True if it's a value is None

Well, I don't exactly know the methods of the CGI module, but there are
ways to access form data in Apache's mod_python module. There you have
a method to access the data from the form, (and it does exactly whet
the CGU module does), and you have an option flag wheter to ignore
empty fields or not.
 
P

Paul Rubin

Atila Olah said:
for key in form.keys():
if not form.keys()[key]: # True if it's a value is None

Ugh! I think you mean

for key, value in form.items():
if not value:

These days you can also say iteritems instead of items and avoid
building a potentially large list in memory.
 
D

Diez B.Roggisch

The second for loop is an attempt at trying to print all the values
that were entered on the for without presenting the hidden values. I'd
really like to do this, but I can't seem to figure out how to make a
special case for hidden form values, nor can I find an example of how
to do it in any of the many python cgi pages I've visited so far. If I

You can't find that out by just processing the http request - http
doesn't know types in form fields. Only HTML(!) does that. But
as you are the one who created the form in the first place, you
should know which from fields are the hidden ones.
Or - if you are more after a generic approach, use namespaces.
You can do that by naming hidden fields e.g. hidden:<name>,
where <name> is of course something like foo or bar or whatever.

Diez
 
G

googleboy

for key in form.keys():Yes, I know which fields are hidden because I
made them that way, but I am trying to figure out a way I can iterate
over the fields and do one thing with hidden fields (assign them to
variables that tell the form how to process) and a different thing with
non hidden fields (present them back to the browser with after
emailling them on to the form owner). I want this script to work fro
several forms on the same webserver, some of which will have several
different and individual names. I am also hopeful that I may be able
to extrapolate from that ways to differentiate other things from my
form input things, but we'll see how it goes.

My html code looks like this:

<h1>Send Feedback</h1>

<form action="cgi/formparse.py" method="get">
<input name="subject" type="text" size="32" maxlength="196">Subject<br>
<input name="name" type="text" maxlength="64">Your name<br>
<input name="email1" type="text" size="24" maxlength="128">Your email
address. <br>
<br><br>Please let me know your thoughts below.<br><textarea
name="thoughts" cols="64" rows="12"></textarea><br>
<input type=submit value="Submit">
<input type="hidden" name="recipient" value="(e-mail address removed)">
<input type="hidden" name="email" value="(e-mail address removed)">
<input type="hidden" name="mailserver" value="localhost">
<input name="Clear" type="button" value="Clear"></form>


In other news, I have tried suggested syntaxes aboce (thanks guys!) but
I get strange errors. If the following:

for key in form.keys():
if not form.keys():
print "<h1> ERROR! <h1><BR>"
print "Please go back and fill in all fields. All fields are
required.<br>"

I get a traceback in teh error_log file that looks like this:

Traceback (most recent call last):
File "cgi/form.py", line 34, in ?
for key, value in form.items():
File "/usr/local/lib/python2.3/cgi.py", line 533, in __getattr__
raise AttributeError, name
AttributeError: items

if this:

for key in form.keys():
if not form.keys()[key]:
print "<h1> ERROR! <h1><BR>"
print "Please go back and fill in all fields. All fields are
required.<br>"
print '</body></html>'

the I get this traceback:

Traceback (most recent call last):
File "/var/www/users/senta/html/gobooks/cgi/form.py", line 35, in ?
if not form.keys()[key]:
TypeError: list indices must be integers

As you can see, I am using python 2.3 (my web service provider is
responsible for this - I'd use 2.4.1 if I could)

TIA

googleboy
 
D

Devan L

googleboy said:
for key in form.keys():Yes, I know which fields are hidden because I
made them that way, but I am trying to figure out a way I can iterate
over the fields and do one thing with hidden fields (assign them to
variables that tell the form how to process) and a different thing with
non hidden fields (present them back to the browser with after
emailling them on to the form owner). I want this script to work fro
several forms on the same webserver, some of which will have several
different and individual names. I am also hopeful that I may be able
to extrapolate from that ways to differentiate other things from my
form input things, but we'll see how it goes.

My html code looks like this:

<h1>Send Feedback</h1>

<form action="cgi/formparse.py" method="get">
<input name="subject" type="text" size="32" maxlength="196">Subject<br>
<input name="name" type="text" maxlength="64">Your name<br>
<input name="email1" type="text" size="24" maxlength="128">Your email
address. <br>
<br><br>Please let me know your thoughts below.<br><textarea
name="thoughts" cols="64" rows="12"></textarea><br>
<input type=submit value="Submit">
<input type="hidden" name="recipient" value="(e-mail address removed)">
<input type="hidden" name="email" value="(e-mail address removed)">
<input type="hidden" name="mailserver" value="localhost">
<input name="Clear" type="button" value="Clear"></form>


In other news, I have tried suggested syntaxes aboce (thanks guys!) but
I get strange errors. If the following:

for key in form.keys():
if not form.keys():
print "<h1> ERROR! <h1><BR>"
print "Please go back and fill in all fields. All fields are
required.<br>"

I get a traceback in teh error_log file that looks like this:

Traceback (most recent call last):
File "cgi/form.py", line 34, in ?
for key, value in form.items():
File "/usr/local/lib/python2.3/cgi.py", line 533, in __getattr__
raise AttributeError, name
AttributeError: items

if this:

for key in form.keys():
if not form.keys()[key]:
print "<h1> ERROR! <h1><BR>"
print "Please go back and fill in all fields. All fields are
required.<br>"
print '</body></html>'

the I get this traceback:

Traceback (most recent call last):
File "/var/www/users/senta/html/gobooks/cgi/form.py", line 35, in ?
if not form.keys()[key]:
TypeError: list indices must be integers

As you can see, I am using python 2.3 (my web service provider is
responsible for this - I'd use 2.4.1 if I could)

TIA

googleboy
['FieldStorageClass', '_FieldStorage__write', '__contains__',
'__doc__', '__getattr__', '__getitem__', '__init__', '__iter__',
'__len__', '__module__', '__repr__', 'bufsize', 'disposition',
'disposition_options', 'done', 'file', 'filename', 'fp', 'getfirst',
'getlist', 'getvalue', 'has_key', 'headers', 'innerboundary',
'keep_blank_values', 'keys', 'length', 'list', 'make_file', 'name',
'outerboundary', 'read_binary', 'read_lines', 'read_lines_to_eof',
'read_lines_to_outerboundary', 'read_multi', 'read_single',
'read_urlencoded', 'skip_lines', 'strict_parsing', 'type',
'type_options']

The form is not a dictionary.
 
D

Diez B.Roggisch

Traceback (most recent call last):
File "/var/www/users/senta/html/gobooks/cgi/form.py", line 35, in ?
if not form.keys()[key]:
TypeError: list indices must be integers

As you can see, I am using python 2.3 (my web service provider is
responsible for this - I'd use 2.4.1 if I could)

That code above can't work - you want something like


if not form.keys() in key:


Try reading the error messages. And google them.

Diez
 
R

Robert Kern

Diez said:
Traceback (most recent call last):
File "/var/www/users/senta/html/gobooks/cgi/form.py", line 35, in ?
if not form.keys()[key]:
TypeError: list indices must be integers

As you can see, I am using python 2.3 (my web service provider is
responsible for this - I'd use 2.4.1 if I could)

That code above can't work - you want something like

if not form.keys() in key:

This thread has to rank somewhere in the top ten threads with respect to
the density of obviously wrong code samples.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
G

googleboy

Robert said:
Diez said:
Traceback (most recent call last):
File "/var/www/users/senta/html/gobooks/cgi/form.py", line 35, in ?
if not form.keys()[key]:
TypeError: list indices must be integers

As you can see, I am using python 2.3 (my web service provider is
responsible for this - I'd use 2.4.1 if I could)

That code above can't work - you want something like

if not form.keys() in key:

This thread has to rank somewhere in the top ten threads with respect to
the density of obviously wrong code samples.

LoL!

Yeah, I am guessing I started out not too far from something that was
doing what I wanted until now, I have gradually moved further and
further away from stuff that looks to me like it should work as python
code. I've even managed to turn code that was working into code that
is broken somehow...

At the moment this bit of code is about validating that stuff was
entered into the form properly, and about doing something with some
types of form inputs and not others. I try all sorts of things and
they just give me exception errors.

And I just don't know what use to make of advice that says stuff like
"The form is not a dictionary." :(

I have spent the whole weekend doing searches and reading tutorials on
this before I posted, I have a form that I get working and doing most
of what I want. It is just the last bits that I can't figure out, and
the tutes don't seem to cover the stuff I am trying to do.

O well, I guess I will go bang my head against it or a few more days
and then hopefully an answer will show up in my brain somewhere.

Thanks for your help.

Googleboy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top