Skip to content Skip to sidebar Skip to footer

Working In Python Cgi With Form. Empty Input Error

I am trying to work with forms on python and I have 2 problems which I can't decide for a lot of time. First if I leave the text field empty, it will give me an error. url like thi

Solution 1:

On the cgi documentation page are these words:

The FieldStorage instance can be indexed like a Python dictionary. It allows membership testing with the in operator

One way to get what you want is to use the in operator, like so:

form = cgi.FieldStorage()

if"userName" in form:
    print"<h1>Hi there, %s!</h1>" % cgi.escape(form["userName"].value)

From the same page:

The value attribute of the instance yields the string value of the field. The getvalue() method returns this string value directly; it also accepts an optional second argument as a default to return if the requested key is not present.

A second solution for you might be:

print"<h1>Hi there, %s!</h1>" % cgi.escape(form.getvalue("userName","Nobody"))

Post a Comment for "Working In Python Cgi With Form. Empty Input Error"