Python validating web forms data
I am able to extract the form field data, but I am NOT able to Put a check / validate the information to see if The field is not empty.
I am writing it in Apache+mod_wsgi, browser FF, Python 2.6, Ubuntu 10.04 64bit
The Following is the sample code where web form field data is read.This is where i need to put a check, whether if this field is EMPTY or NOT. If empty ,a default_error string for specific field, go back and fill in the data. Till then "Save" ( or "Print & Save") DOES NOT show.
state = form.getvalue('state')
nameDistrict = form.getvalue('district')
dist_code = form.getvalue('Dcode')
html += 'State :' + str(state)
html += 'Distr开发者_StackOverflowict Name :' + str(nameDistrict)
html += 'District Code :' + str(dist_code)
The following is one way I was trying, nAdA. :(
def val(dict, key, default_str):
value = dict.get(key, default_str)
if value == '':
return default_str
else:
return value
second attempt
if state == ' ' :
return 'Please Enter the State Name'
else :
return state
another trial was
try:
state = form.getvalue('state')
except ValueError:
'Please Enter the state Name'
No luck.
Please show by some examples by which I can put the check.
Thanks
This is the part of the code I am using.
class Handler:<br> def do(self, environ, start_response):<br> html = """ <html><head><title>C Informatics</title> <meta charset=utf-8> <form method="post" action="newreg.py"><br> <table border=1><br> <tr><td>State:-<input type="text" name="state" size=15 max length=15><br> City/District:<input type="text" name="district">Code:<input type="text" > name="Dcode" max length=10 size=10> . . . . . . . . .
<input type="submit" Value="Save">
<input type="submit" value="Print & Save"></center>
</form>
This question was also asked on mod_wsgi list and answer given there.
http://groups.google.com/group/modwsgi/browse_thread/thread/97e1ab3bde4230a1#
精彩评论