Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to check parameters and use WTForms in Flask

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

It is believed that many inexperienced people have no idea about how to calibrate parameters and use WTForms in Flask. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

One: the necessity of parameter verification

When you process POST requests submitted by browsers with parameters, such as a user registration function, the site will limit the length of passwords submitted by users. At this time, if you write a custom parameter check in the view function, the view code will quickly become difficult to read.

Especially when there are a lot of parameters, the code becomes more difficult to maintain. There are some libraries in Flask that can simplify this work. Wtforms is one of the excellent libraries. It extends and adds some exquisite help functions that are readily available on the basis of Flask-WTF. These functions will make you get twice the result with half the effort in the application!

Second: the use of Wtforms 2.1 basic use

To use this library, we first need to install it

Pip install wtforms

Post the code directly and feel its usage. I'll explain the key points of the code later.

Form function

You can see that the interface registered in the form function has five parameters, and the following StringField and PasswordField represent their parameter types. So how does it validate the parameters?

The answer is the validators keyword parameter, which receives an array. For example, the email parameter in the above code is required, and its length is limited to 6-35 bits, and the length of the parameter password password is also limited.

Carefully, you may also have found the confirmation password parameter confirmPassword

The EqualTo validation function is used to verify that password and confirmPassword are the same.

Through the above classes, it is very simple to verify the form parameters, instead of writing our own functions manually to verify the form parameters one by one. In addition to the above parameter types and validation functions, Wtforms provides many other types and functions, let's take a look!

2.4 Field Typ

The types of fields supported by Wtforms are very rich, and the description is very detailed. You can import them according to your own needs.

2.3 Verification function

Form validation functions supported by WTForms

2.4 perform verification

We defined fields and validations in the RegisterForm class earlier, so how do we use them in view functions?

View

Quite simply, import the form form you just defined and call its validation function validate () to perform the verification, which returns a Boolean value. So just judge it and it's over!

After reading the above, have you mastered how to check the parameters in Flask and how to use WTForms? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report