Decision (if, else) routine is not working as intended with CGImodule

E

epsilon

All:

I'm running into trouble figuring this one out. It seems that my
decision routine is not working as intended. Does anyone know why my
output continues to utilize the "else" portion of the routine.

Thank you,
Christopher

++++++++++

#!/usr/bin/python

import cgi

print "Content-type: text/plain\n"
tag_form = cgi.FieldStorage(keep_blank_values=True)

#if not tag_form.has_key("fse00"):
if tag_form["fse00"] == "":
fse000 = {"fse00": "0"}
tag_form.update(fse000)
print "Printing fse000: ", tag_form["fse00"]
else:
print "Printing fse00: ", tag_form["fse00"]
 
G

Gabriel Genellina

I'm running into trouble figuring this one out. It seems that my
decision routine is not working as intended. Does anyone know why my
output continues to utilize the "else" portion of the routine.

tag_form = cgi.FieldStorage(keep_blank_values=True)

#if not tag_form.has_key("fse00"):
if tag_form["fse00"] == "":

tag_form["fse00"] is a FieldStorage instance, not a string. To get its
value, use:

if tag_form["fse00"].value == ""
if tag_form.getvalue("fse00")==""
if tag_form.getfirst("fse00")==""

See http://docs.python.org/lib/module-cgi.html
 
B

bruno.desthuilliers

All:

I'm running into trouble figuring this one out. It seems that my
decision routine is not working as intended. Does anyone know why my
output continues to utilize the "else" portion of the routine.

Probably because the test expression 'tag_form["fse00"] == ""' evals
to false ?

Hint: try printing out tag_form["fse00"] before the test, ie:


(snip)
#!/usr/bin/python

import cgi

print "Content-type: text/plain\n"
tag_form = cgi.FieldStorage(keep_blank_values=True)

print "tag_form[\"fse00\"] is actually: %s" % tag_form["fse00"]
print "tag_form[\"fse00\"] == '' evals to: %s" % (tag_form["fse00"]
== '')
if tag_form["fse00"] == "":
fse000 = {"fse00": "0"}
tag_form.update(fse000)
print "Printing fse000: ", tag_form["fse00"]
else:
print "Printing fse00: ", tag_form["fse00"]

HTH
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top