Categories
Python Answers

How to cast Python Flask form value to int?

Spread the love

Sometimes, we want to cast Python Flask form value to int.

In this article, we’ll look at how to cast Python Flask form value to int.

How to cast Python Flask form value to int?

To cast Python Flask form value to int, we call the int function.

For instance, we write

@app.route("/getpersonbyid", methods=["POST"])
def get_person_by_id():
    personId = int(request.form["person-id"])
    return str(personId)

to create the /getpersonbyid route with the get_person_by_id function.

In it, we get the form value from the form field with the name attribute set to person-id from request.form.

Then we convert the value to an int with int.

Conclusion

To cast Python Flask form value to int, we call the int function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *