Getting a Form To Work

MRAB python at mrabarnett.plus.com
Sun Jul 19 14:13:13 EDT 2009


Victor Subervi wrote:
> When the form comes up the first time, there is the default value for 
> num. When I fill in a number in the form and press send, even though the 
> form sends to itself (same page name), I would think it would read the 
> number sent. Here again is the code:
> 
> from primeNumbers import primeNumbers
> 
> try:
>   lang = form.getfirst('lang', 'en')
>   browser = form.getfirst('browser', 'all')
>   site = form.getfirst('site', 'bridge')
>   num = form.getfirst('num','')
> except:
>   pass
> 
> ourFile = string.split(__file__, "/")
> p = ourFile[len(ourFile) - 1]
> p = p[: - 9]
> site = ourFile[4][:-10]
> if site != '':
>   site = site[:-1]
> 
> print "Content-Type: text/html"
> print
> print """
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" 
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
> <head xmlns="http://www.w3.org/1999/xhtml">
> </head>
> """
> if num != '':
>   num = round(float(num))
>   roots = primeNumbers(num)
>   print roots
> if num == '':
>   print """
> <form action="http://13gems.com/test-Calculators_frame.py" method="post">
> <input type="text" name="num" size="20" maxlength="20" value="Enter 
> number here..." onfocus="this.value=''" />
> <input type="image" src="http://13gems.com/images/search.jpg" 
> name="search" id="search" />
> </form>
> """
> print '</body></html>\n'
> 
> And why not use bare excepts? What is better?
> 
[snip]
An exception could be raised for a number of reasons, including
misspelling a name. You should catch only those exceptions you expect so
that others you don't expect, caused by bugs, will still be shown;
you'll then know there's a bug and can fix it.



More information about the Python-list mailing list