use:
str.isdigit()
http://www.tutorialspoint.com/python/string_isdigit.htm
what is idDigit() ? is that a call to a method somewhere else.
and arent you trying to validate numberofapples = request.POST['numberofapples']
not playerid?
Personally if this is form data you are trying to validate i'd write a
form class and use the form clean() method to evaluate them as
integerfields:
http://docs.djangoproject.com/en/1.2/ref/forms/fields/
http://docs.djangoproject.com/en/1.2/topics/forms/
Also you can verify using regular expressions *good to get comfortable
with if you're new to django.
cheers
sam_w
On Mon, Mar 28, 2011 at 4:59 PM, django beginner <djangopyth@gmail.com> wrote:
> Hi django experts,
>
> just want to know your opinion on how to validate the user input
> (example: the user input field should be integer, but the user
> accidentally inputs any character), here is my code:
>
> FYI; the numberofapples field is IntegerField
>
> here is my sample code:
>
> def apples_edit(request):
> if request.POST['id']:
> id = request.POST['id']
> numberofapples = request.POST['numberofapples']
> conn = psycopg2.connect("dbname=my_db user=user password=pass
> host=localhost")
> cur = conn.cursor()
>
> try:
> if isDigit(playerid):
> pass
> else:
> template_name = 'err_template.html'
> t = loader.get_template(template_name)
> c = RequestContext(request, {"err_msg":"number of
> apples should be integer, Please correct your input"})
> return HttpResponse(t.render(c))
> except ValueError:
> template_name = 'err_template.html'
> t = loader.get_template(template_name)
> c = RequestContext(request, {"err_msg":"number of apples
> should be integer, Please correct your input"})
> return HttpResponse(t.render(c))
>
> sql = """ UPDATE my_db SET "numberofapples"='%s' where "id"='%s'
> """ % (numberofapples,id)
> cur.execute(sql)
> conn.commit()
> conn.close()
>
> ----------------------
> My problem is that, the error template does not appear after I type in
> any character for numberofapples field.
> What would be the correct code for this?
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment