By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Processing an XHTML form using CGI script

P: 3
I'm having problems casting input information from an XHTML form as a different type in a CGI script using Python.
For just a simple example I'd use the XHTML code:
[HTML]<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html>
<head>
<title>formTest</title>
</head>
<body>
<form action="./cgi-bin/formTest.py" method="post">
<p>
Calculate:<br />
<input type="text" name="num1" /> + <input type="text" name="num2" /><br />
<input type="submit" value="Calculate" />
</p>
</form>
</body>
</html>[/HTML]

And the Python Code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2.  
  3. import cgi
  4. import cgitb; cgitb.enable()
  5.  
  6. def main():
  7.  
  8.         form = cgi.FieldStorage()
  9.  
  10.         num1 = form.getvalue("num1")
  11.         num2 = form.getvalue("num2")
  12.  
  13.         sum = int(num1) + int(num2)
  14.  
  15.         print "content-type:  text/html\n"
  16.  
  17.         print "<?xml version = \"1.0\" encoding = \"utf-8\"?>"
  18.         print "<!DOCTYPE html PUBLIC \"-//w3c//DTD XHTML 1.1//EN\""
  19.         print "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
  20.         print "<html>"
  21.         print "  <head>"
  22.         print "    <title>Solution</title>"
  23.         print "    <style type=\"txt/css\">"
  24.         print "  <head>"
  25.         print "  <body>"
  26.  
  27.         print "    <p>Solution: " + str(sum) + "</p>"
  28.  
  29.         print "  </body>"
  30.         print "</html>"
  31.  
  32. main()
  33.  
However when I try to cast num1 and num2 as integers, I get an error saying that only string or number arguments can be used:

int() argument must be a string or a number

And if I don't cast them I get an error saying:

unsupported operand types for +: 'NoneType' and 'NoneType'

Is there a way to cast the input variables from the XHTML form as something other that 'NoneType' so that I can use them in calculations?
Dec 1 '06 #1
Share this Question
Share on Google+
2 Replies


bartonc
Expert 5K+
P: 6,596
'NoneType' is the type of None.
Python converts many 'null' values to None (for example a SQL database item that is a null value gets returned in python as None). I appears to me (I don't read html) that there is no value in you form.
Dec 1 '06 #2

bartonc
Expert 5K+
P: 6,596
A common practice is
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     num1 = int(form.getvalue("num1"))
  3. except (TypeError, ValueError):
  4.     num1 = 0    # or warn the user somehow that the field is empty
Dec 1 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.