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 can use the int function.

For instance, we write

personId = int(request.form['personId'])

to call int with the input value of the field with name personId into an integer.

We can also call request.form.get with the type argument to do the same thing.

For instance, we write

personId = request.form.get('personId', type=int)

to get the same input field value and convert it to an int by setting the type argument to int.

Conclusion

To cast Python Flask form value to int, we can use 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 *