繁体   English   中英

从 WTForms 表单中获取数据

[英]Get data from WTForms form

提交后如何从 WTForms 表单中获取数据? 我想获取在表单中输入的电子邮件。

class ApplicationForm(Form):
    email = StringField()

@app.route('/', methods=['GET', 'POST'])
def index():
    form = ApplicationForm()

    if form.validate_on_submit():
        return redirect('index')

    return render_template('index.html', form=form)
<form enctype="multipart/form-data" method="post">
    {{ form.csrf_token }}
    {{ form.email }}
    <input type=submit>
</form>

每个字段都有一个包含处理数据的data属性。

the_email = form.email.data

使用表单数据在入门文档中进行了描述。

您最可能使用 Form.attrs 做事的地方是在index函数中。 我在方法参数上添加了一些条件保护。 如果他们也使用GETPOST ,您想要做不同的事情。 还有其他方法可以完成所有这些,但我不想立即改变太多。 但是你应该这样想清楚。 如果我没有表单数据,因为我刚刚提出了初始请求,我将使用GET 在模板中呈现表单后,我将发送一个POST (正如您在模板顶部看到的那样)。 所以我需要先处理这两个案例。

然后,一旦表单被呈现并返回,我将有数据或没有数据。 因此处理数据将发生在控制器的POST分支中。

@app.route('/index', methods=['GET', 'POST'])
def index():
    errors = '' 

    form = ApplicationForm(request.form)
    if request.method == 'POST':
        if form.is_submitted():
            print "Form successfully submitted"
        if form.validate_on_submit():
            flash('Success!')
            # Here I can assume that I have data and do things with it.
            # I can access each of the form elements as a data attribute on the
            # Form object.
            flash(form.name.data, form.email.data)
            # I could also pass them onto a new route in a call.
            # You probably don't want to redirect to `index` here but to a 
            # new view and display the results of the form filling.
            # If you want to save state, say in a DB, you would probably
            # do that here before moving onto a new view.
            return redirect('index')
        else:  # You only want to print the errors since fail on validate
            print(form.errors)  
            return render_template('index.html',
                                   title='Application Form',
                                   form=form)
    elif request.method == 'GET':
        return render_template('index.html', 
                               title='Application Form',
                                   form=form)

为了提供帮助,我从我的一些工作代码中添加了一个简单的示例。 鉴于您的代码和我的演练,您应该能够遵循它。

def create_brochure():
    form = CreateBrochureForm()
    if request.method == 'POST':
        if not form.validate():
            flash('There was a problem with your submission. Check the error message below.')
            return render_template('create-brochure.html', form=form)
        else:
            flash('Succesfully created new brochure: {0}'.format(form.name.data))
            new_brochure = Brochure(form.name.data,
                                    form.sales_tax.data,
                                    True,
                                    datetime.datetime.now(),
                                    datetime.datetime.now())
            db.session.add(new_brochure)
            db.session.commit()
            return redirect('brochures')
    elif request.method == 'GET':
        return render_template('create-brochure.html', form=form)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM