flask版
.py
from flask import Flask, render_template, request, session, current_app, g, redirect from wtforms import Form from wtforms.fields import simple from wtforms.fields import html5 from wtforms.fields import core from wtforms import widgets from wtforms import validators app = Flask(__name__) class RegisterForm(Form): name = simple.StringField( label='用户名', validators=[ validators.DataRequired(message='用户名不能为空') ], widget=widgets.TextInput(), render_kw={'class': 'form-control'}, default='jocket' ) pwd = simple.PasswordField( label='密码', validators=[ validators.DataRequired(message='密码不能为空'), ], widget=widgets.PasswordInput(), render_kw={'class': 'form-control'} ) pwd_confirm = simple.PasswordField( label='确认密码', validators=[ validators.DataRequired(message='密码不能为空'), validators.EqualTo('pwd', message='两次密码输入不一致') ], widget=widgets.PasswordInput(), render_kw={'class': 'form-control'} ) email = html5.EmailField( label='邮箱', validators=[ validators.DataRequired(message='邮箱不能为空'), validators.Email(message='邮箱格式错误') ], widget=widgets.TextInput(input_type='email'), render_kw={'class': 'form-control'} ) gender = core.RadioField( label='性别', choices=( (1, '男'), (2, '女') ), coerce=int ) city = core.SelectField( label='城市', choices=( ('bj', '北京'), ('sh', '上海'), ) ) hobby = core.SelectMultipleField( label='爱好', choices=( (1, '篮球'), (2, '足球'), ), coerce=int ) favor = core.SelectMultipleField( label='喜好', choices=( (1, '篮球'), (2, '足球'), ), widget=widgets.ListWidget(prefix_label=False), option_widget=widgets.CheckboxInput(), coerce=int, default=[1, 2] ) class LoginForm(Form): name = simple.StringField( validators=[ validators.DataRequired(message="用户名不能为空"), # validators.Length(min=6, max=18, message="用户名长度必须大于%(min)d且小于%(max)d") ], render_kw={"placeholder": '请输入用户'} ) pwd = simple.PasswordField( validators=[ validators.DataRequired(message="密码不能为空"), # validators.Length(min=6, max=18, message='密码长度必须大于%(min)d'), # validators.Regexp(regex="^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[$@$15*?&])[A-Za-zd$@$!%*?&]{8,}", # message="密码至少8个字符,至少1个大写字母, 1个小写字母,1个数字和1个特殊字符") ], render_kw={"placeholder": "请输入密码"} ) @app.route('/register', methods=['GET', 'POST']) def register(): if request.method == 'GET': form = RegisterForm() return render_template('register.html', form=form) form = RegisterForm(formdata=request.form) if form.validate(): print(form.data) return redirect('/login') @app.route('/login', methods=['GET', "POST"]) def login(): if request.method == "GET": form = LoginForm() print(form.name, form.pwd) # <input/> return render_template('login.html', form=form) form = LoginForm(formdata=request.form) if form.validate(): print("验证成功") print(form.data) return 'data' else: print(form.errors) return render_template('login.html', form=form) if __name__ == '__main__': app.run(debug=True)
.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>title</title> </head> <body> <form action="/login" method="post" novalidate> <p> {{form.name}} </p> {{form.name.errors.0}} <p> {{form.pwd}} </p> {{form.pwd.errors.0}} <p> <input type="submit" value="提交"> </p> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <form action="" method="" novalidate> {% for i in form %} <p> {{i.label}}: {{i}} {{i.errors[0]}} </p> {% endfor %} <input type="submit" value="提交"> </form> </body> </html>