获取数据自动从MongoEngine模型生成WTForms

时间:2017-04-02 13:03:40

标签: flask wtforms flask-mongoengine

如何在提交后从WTForms表单中获取数据?

型号:

class User(db.Document):
    name = db.StringField(required=True)
UserForm = model_form(User)

查看:

@app.route('/submit', methods=('GET', 'POST'))
def add_user():
    form = UserForm()
    if form.form.validate_on_submit():
        print(form.name.data)
return render_template('user.html', form=form)

以下是HTML格式:

<form method="POST" action="">
    {{ form.hidden_tag() }}
    {{ form.name.label }} {{ form.name(size=20) }}
    <input type="submit">
</form>

1 个答案:

答案 0 :(得分:0)

Form Validation with WTForms

@app.route('/submit', methods=('GET', 'POST'))
def add_user():
    form = UserForm(request.form)
    if request.method == 'POST' and form.validate():
        print(form.name.data)
return render_template('user.html', form=form)