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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to