Flask之WTfroms组件

Posted qq631243523

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask之WTfroms组件相关的知识,希望对你有一定的参考价值。

一、WTfroms简介

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/[email protected]/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>

 

以上是关于Flask之WTfroms组件的主要内容,如果未能解决你的问题,请参考以下文章

flask wtforms组件详解

Flask 知识总结

Flask之模板之宏继承包含

Flask框架web开发

Flask组件之Flask-session

flask之wtform与flask-session组件