Operating environment:
python2.7
flask 0.11
flask-wtf 0.14.2
wtform can define some fields through a class, these fields will generate labels on the front end, and automatically determine the format of the front-end input data by setting the field validation rules.
Generally used for user login, user registration and other information input.
1 from wtforms import Form 2 from flask import Flask,render_template,request,redirect 3 from wtforms.fields import core 4 from wtforms.fields import html5 5 from wtforms.fields import simple 6 from wtforms import validators 7 from wtforms import widgets 8 import sys 9 reload(sys) 10 sys.setdefaultencoding(' utf8 ' ) 11 # For python2.7 transcoding, to avoid displaying Chinese as garbled characters 12 13 14 app = Flask( __name__ ,template_folder= ' templates ' ) 15 app.debug= True 16 17 class LoginForm(Form): 18 name = simple.StringField( 19 label= ' username ' , #The front end displays Chinese 20 validators= [ 21 validators.DataRequired(message= ' username cannot be empty ' ), 22 validators.Length(min=6,max=18,message= ' The length of the username must be greater than %(min)d and less than %(max)d ' ) 23 ], #Validation rules of the field 24 widget=widgets.TextInput( ), #Label displayed on the page 25 render_kw={ ' class ' : ' form=control ' } #Add attributes to the above plugins, such as bootstrap attributes 26 ) 27 pwd = simple.PasswordField( 28 label= ' password ' , 29 validators= [ 30 validators.DataRequired(message= ' The password cannot be empty ' ), 31 validators.Length(min=8,message= ' The length of the username must be greater than %(min)d ' ), 32 validators.Regexp(regex= " ^(? =.*[az])(?=.*[AZ])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@ $!%*/&]{8,} " , 33 message= ' Password at least 8 characters, at least 1 uppercase letter, 1 lowercase letter ' ), 34 35 ], 36 widget= widgets.PasswordInput(), 37 render_kw={ ' class ' :' from-control ' } 38 ) 39 #Field , which contains regular expressions 40 41 @app.route( ' /login ' ,methods=[ ' GET ' , ' POST ' ]) 42 def login(): 43 if request .method == ' GET ' : 44 form = LoginForm() 45 return render_template( ' login.html ' , form= form) 46 #Pass the form object to the front end 47 else : 48 form = LoginForm(formdata=request.form) #Get the value from the request body from 49 if form.validate(): #Validate 50 print " The user submitted the data through, and the submitted value is: " ,form.data 51 else : 52 print form.errors #Error message 53 return render_template ( ' login.html ' ,form= form) 54 55 56 57 @app.route( ' / ' ) 58 def hello_world(): 59 return 'Hello World!' 60 61 62 if __name__ == '__main__': 63 app.run()
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="x-ua-compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <title>Title</title> 8 </head> 9 <body> 10 <h1>登陆</h1> 11 <form method="post"> 12 <p>{{ form.name.label }} {{ form.name }} {{ form.name.errors[0] }}</p> 13 <p>{{ form.pwd.label }} {{ form.pwd }} {{ form.pwd.errors[0] }}</p> 14 <input type="submit" value="提交"> 15 </form> 16 </body> 17 </html>
In the front end, the value in the field and the error information in the value are directly obtained through the Form object. We generally take the first error information, and we can already tell the user that the information is wrong.