from flask import Flask,request,render_template
from wtforms import Form,StringField
from wtforms import validators
from wtforms.validators import Length,EqualTo class RegisterForm(Form):
username = StringField(validators=[Length(max=10,min=3,message='用户名长度不正确')])
password = StringField(validators=[Length(max=10,min=3,message='密码长度不正确')])
password_repeat = StringField(validators=[Length(max=10,min=3,message='密码长度不正确'),EqualTo('password')]) app = Flask(__name__) @app.route('/')
def hello_world():
return 'Hello World!' @app.route('/register/',methods=['get','post'])
def register():
if request.method == 'GET':
return render_template('register.html')
else:
# username = request.form.get('username')
# password = request.form.get('password')
# password_repeat = request.form.get('password_repeat')
# if 3 > len(username) or len(username)> 10:
# return '用户名长度不正确'
# if 3 > len(password) or len(password)> 10:
# return '密码长度不正确'
# if password != password_repeat:
# return '密码输入不一致'
form = RegisterForm(request.form)
if form.validate():
return 'success'
else:
print(form.errors)#{'username': ['用户名长度不正确'],
# 'password': ['密码长度不正确'], 'password_repeat': ['密码长度不正确']}
#因为是字典,获取方式我就不写了
return 'fail' if __name__ == '__main__':
app.run(debug=True)

wtforms 简单使用-LMLPHP

05-14 01:45