Home > database >  REQUEST FUNCTION FROm FLASK
REQUEST FUNCTION FROm FLASK

Time:11-17

I am using request.form.get("name") to get the value of an input whose type=number.

I then want to be able to compare so as to ensure that it is a postive number as follows:

number = request.form.get("name")

If number < 0:

# Do something

  1. This gives an error as follows:-

TypeError: < not supported between instances of 'str' & 'int'

Doesnt the input field already make the said request a number?

what is happening??

I tried to compare two numbers, turns out one of them is being treated as a string.

CodePudding user response:

HTML input types like <input type="number"> serve only for client-side validation purposes and have no effect on what's sent in the HTTP request. The HTTP forms will always contain text, like param1=123&param2=blah. You need to parse the values explicitly:

try:
    number = int(request.form.get('name'))
except ValueError:
    # handle the error

For more complex cases you can use a form validation library, e.g. Flask-WTF.

  • Related