473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Processing an XHTML form using CGI script

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
2 2469
bartonc
6,596 Expert 4TB
'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
6,596 Expert 4TB
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

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

Similar topics

1
by: webworldL | last post by:
Has anybody had any luck processing XHTML1.1 documents with xml.sax? Whenever I try it, python loads the W3C DTD from the top, then crashes saying that there's an error in the external DTD. All I...
1
by: Eric Anderson | last post by:
I am trying to use XSLT to extend XHTML so that I can have custom widgets. For example I have a calendar control. Whenever I put the following in the psudo-XHTML doc: <calendarbox name="mycal"/>...
12
by: hawat.thufir | last post by:
I'm trying do some "screen scraping", and am using <http://www.oreilly.com/catalog/xmlhks/> for inspiration. First I'd like to convert XHTML to XML, or extract XML from XHTML, I'm not sure how...
16
by: Mcginkel | last post by:
I am trying to find a way to load XHTML content in an Iframe. I use to do this in html by using the following code : var iframeObject = document.createElement("iframe");...
82
by: Buford Early | last post by:
I read this in http://annevankesteren.nl/2004/12/xhtml-notes "A common misconception is that XHTML 1.1 is the latest version of the XHTML series. And although it was released a bit more than a...
12
by: Alex D. | last post by:
How can I stop asp.net from rendering XHTML istead of HTML? My javascripts are rendering wrong because of that. It is rendering &amp; to clients instead of &. Any help? Thanks, Alejandro.
11
by: Michael Powe | last post by:
How can I make an XHTML-compliant form of an expression in this format: document.write("<scr"+"ipt type='text/javascript' src='path/to/file.js'>"+"</scr"+"ipt>"); this turns out to be a...
6
by: Dick Watson | last post by:
I had a page that works when setup like this: === <form name="frmCalc" action=""> <script type="text/javascript"> function btnCalc_onclick(abc) { return "got here with " + abc; }
1
by: EpicOfChaos | last post by:
Hey guys I am having a bit of trouble, I am trying to process a form(to upload a image) to a hidden iframe. and I am getting a error. The error is : Method Not Allowed The requested method POST is...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.