标签:choices meta label ror containe 确认密码 post password cti
WTForms插件是类似于django的form组件的插件,可以帮我们写标签,校验数据等。
安装:
pip install WTForms
使用:
from wtforms import Form from wtforms.fields import simple, core from wtforms import validators class RegForm(Form): username = simple.StringField( label="用户名", validators=[ validators.DataRequired(message="数据不能为空!"), validators.length(min=4, max=6, message="用户名不能少于4位且不能大于6位") ], render_kw={"class": "form-control", "pp": "88"} ) password = simple.PasswordField( label="密码", validators=[ validators.DataRequired(message="密码不能为空!"), validators.length(min=6, max=16, message="密码不能少于6位且不能大于6位"), validators.Regexp(regex="\d+", message="密码必须是数字") ], render_kw={"class": "form-control", "pp": "88"} ) re_password = simple.PasswordField( label="确认密码", validators=[ validators.EqualTo(fieldname="password", message="两次密码不一致") ], render_kw={"class": "form-control"} ) gender = core.RadioField( label="性别", validators=[ validators.DataRequired(message="请先选择一个性别!") ], choices=((1, "男"), (2, "女")), coerce=int, # default=1, ) hobby = core.SelectMultipleField( label="爱好", choices=((1, "游戏"), (2, "妹子"), (3, "汉子")), coerce=int, default=[1, 2], render_kw={"class": "form-control"} ) @app.route("/register", methods=("GET", "POST")) def reg(): form_obj = RegForm() if request.method == "POST": print(request.form) form_obj = RegForm(request.form) if form_obj.validate(): ers = request.form.to_dict() print(ers) print(form_obj.data) return "注册成功" return render_template("reg.html", form_obj=form_obj) if __name__ == ‘__main__‘: app.run(debug=True)
前端:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="row"> <div class="col-md-4 col-md-offset-4"> <form action="" method="post" novalidate> {% for field in form_obj %} <div class="form-group"> {{ field.label }} {{ field }} {{ field.errors.0 }} </div> {% endfor %} <div> <button class="btn btn-primary btn-lg btn-block">注册</button> </div> </form> </div> </div> </div> </body> </html>
标签:choices meta label ror containe 确认密码 post password cti
原文地址:https://www.cnblogs.com/qq631243523/p/10268873.html