Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Some newbie cgi form questions...

Reply
Thread Tools

Some newbie cgi form questions...

 
 
googleboy
Guest
Posts: n/a
 
      08-07-2005
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>'

 
Reply With Quote
 
 
 
 
Atila Olah
Guest
Posts: n/a
 
      08-07-2005
>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.

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      08-07-2005
"Atila Olah" <(E-Mail Removed)> writes:
> 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.
 
Reply With Quote
 
Diez B.Roggisch
Guest
Posts: n/a
 
      08-07-2005
> 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

 
Reply With Quote
 
googleboy
Guest
Posts: n/a
 
      08-07-2005
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 Removed)">
<input type="hidden" name="email" value="(E-Mail 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

 
Reply With Quote
 
Devan L
Guest
Posts: n/a
 
      08-07-2005

googleboy wrote:
> 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 Removed)">
> <input type="hidden" name="email" value="(E-Mail 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


>>> import cgi
>>> form = cgi.FieldStorage()
>>> dir(form)

['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.

 
Reply With Quote
 
Diez B.Roggisch
Guest
Posts: n/a
 
      08-07-2005

> 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



 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      08-07-2005
Diez B.Roggisch wrote:
>>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
http://www.velocityreviews.com/forums/(E-Mail Removed)

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

 
Reply With Quote
 
googleboy
Guest
Posts: n/a
 
      08-08-2005

Robert Kern wrote:
> Diez B.Roggisch wrote:
> >>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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript for Form (using CGI formmail) that sends user a copy ofthe form data TPK Javascript 1 08-27-2010 12:49 PM
CGI (read multipart form): Accept-Charset encoding error (CGI::InvalidEncoding) Stefan Fischer Ruby 2 02-23-2010 08:17 AM
I need some help with browser output in ruby cgi - cgi.rb Professor Chen Ruby 0 02-06-2006 07:08 AM
New Service for CGI form submissions - Form-Submit.com ultranate@gmail.com ASP General 0 01-12-2006 08:21 PM
Re: CGI Perl "use CGI" statement fail Jürgen Exner Perl 0 07-31-2003 02:00 PM



Advertisments