当前位置:网站首页>Flask learning and project practice 9: WTF form verification
Flask learning and project practice 9: WTF form verification
2022-07-06 03:36:00 【Study notes of Zhou】
1.WTF Introduction to form validation
Flask-WTF It's simplified WTForms A third-party library for operations .WTForms The two main functions of the form are to verify the legitimacy of the data submitted by the user and render the template . Of course, it also includes some other functions :CSRF Protect , File upload, etc . install Flask-WTF It will also be installed by default WTForms, So use the following command to install Flask-WTF: pip install flask-wtf.
For example, log in when you go to Taobao , First check whether the user input meets the conditions , If you are not satisfied, you will not check the database . namely Verify whether the requirements are met before querying the database .
WTForms It can also be used in other frameworks , Such as django etc. . and Flask This third-party library is specially simplified , To facilitate the operation of the project .
2. Perform form validation
validators Just pass some validation parameters in for comparison .
The code is as follows :
class RegistForm(Form):
name = StringField(validators=[length(min=4,max=25)])
email = StringField(validators=[email()])
password = StringField(validators=[DataRequired(),length(min=6,max=10),EqualTo('confirm')])
confirm = StringField()
DataRequired It means that the password must be entered ,EqualTo(‘confirm’) Must be equal .
3. Code implementation

stay action Which means , When the button is clicked , To which url.
method It refers to the request method . And also notice that input Of name It needs to correspond to your own settings .


If the authenticator of the mailbox is missing , have access to pip install email_validator To install .
At the same time, the above code needs to be improved , That is, for views, only GET, Need to increase the post As shown in the figure below :

So you can verify .( Initially relieve the pressure on the database ).
边栏推荐
- BUAA magpie nesting
- Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
- Redo file corruption repair
- Yyds dry inventory what is test driven development
- Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
- Python implementation of maddpg - (1) openai maddpg environment configuration
- Overview of super-resolution reconstruction of remote sensing images
- JS Vanke banner rotation chart JS special effect
- 3.1 rtthread 串口设备(V1)详解
- SAP ALV单元格级别设置颜色
猜你喜欢
随机推荐
BUAA calculator (expression calculation - expression tree implementation)
【Qt5】Qt QWidget立刻出现并消失
Cross origin cross domain request
Recommended foreign websites for programmers to learn
Redis cache breakdown, cache penetration, cache avalanche
Four logs of MySQL server layer
A brief introduction to symbols and link libraries in C language
施努卡:3d视觉检测应用行业 机器视觉3d检测
1、工程新建
[risc-v] external interrupt
深入刨析的指针(题解)
Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
Force buckle 1189 Maximum number of "balloons"
3.2 detailed explanation of rtthread serial port device (V2)
1.16 - 校验码
Redo file corruption repair
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
These are not very good
Lua uses require to load the shared library successfully, but the return is Boolean (always true)
Pytorch基础——(1)张量(tensor)的初始化









