validation

76 views
Skip to first unread message

nitin chandra

unread,
Aug 24, 2011, 4:40:51 AM8/24/11
to mod...@googlegroups.com
Hello All,

The last time I wrote a mail to Python Tutors by pasting some of my
code, I was asked 'where is this kind of coding done?' ...

so I am back asking some thing pythonic on this list.

This is the value I am taking from the Form Field (with Jquery /
Autocomplete), but how do I validate / check to see if the value
entered in the Field is NOT NULL.


state = form.getvalue('state')

html += '<tr><td>State :</td><td><center>' + str(state) + '</td></tr>'

The above 2 line are the actual code.


I tried If / else, did not work

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'

Does not WORK.

I need to Check / Validate the data entered before inserting into DB.

Please guide.

Thanks

Nitin

Graham Dumpleton

unread,
Aug 24, 2011, 4:59:22 AM8/24/11
to mod...@googlegroups.com
This has got nothing to do with mod_wsgi and perhaps is better asked
on StackOverflow site. A few suggestions below though.

On 24 August 2011 18:40, nitin chandra <nitinc...@gmail.com> wrote:
> Hello All,
>
> The last time I wrote a mail to Python Tutors by pasting some of my
> code, I was asked 'where is this kind of coding done?' ...
>
> so I am back asking some thing pythonic on this list.
>
> This is the value I am taking from the Form Field (with Jquery /
> Autocomplete), but how do I validate / check to see if the value
> entered in the Field is NOT NULL.
>
>
>  state = form.getvalue('state')

Add debugging statements to your code

Some useful ones in this context to do are:

print repr(state)
print type(state)

The repr function is useful because if it is a string, it will quote
it and so more obvious it is a string.

The type function prints the type.

>>> s = ''
>>> print s

>>> print repr(s)
''
>>> print type(s)
<type 'str'>
>>> s = None
>>> print s
None
>>> print repr(s)
None
>>> print type(s)
<type 'NoneType'>

Look in the web server log file for what is printed. Then you can work
out what it is set to when not supplied. Likely it is None and you
actually want:

if state is None:
... not supplied
else:
... supplied

And sure enough, looking at the code for getvalue() in Python source one finds:

def getvalue(self, key, default=None):
"""Dictionary style get() method, including 'value' lookup."""
if key in self:
value = self[key]
if type(value) is type([]):
return map(attrgetter('value'), value)
else:
return value.value
else:
return default

So, should be None. If necessary you could actually provide a
'default' argument to change it something other than None if you
really needed to.

A few suggestions.

1. Always check the documentation thoroughly.

2. Add debug statements and print out values and types of variables if
needed to work out what they are set to.

3. Grab down the Python source code and dig around in the source code
if still not sure.

Graham

> html += '<tr><td>State :</td><td><center>' + str(state) + '</td></tr>'
>
> The above 2 line are the actual code.
>
>
> I tried If / else, did not work
>
> 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'
>
> Does not WORK.
>
> I need to Check / Validate the data entered before inserting into DB.
>
> Please guide.
>
> Thanks
>
> Nitin
>

> --
> You received this message because you are subscribed to the Google Groups "modwsgi" group.
> To post to this group, send email to mod...@googlegroups.com.
> To unsubscribe from this group, send email to modwsgi+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/modwsgi?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages