从 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

getting started doc.

中描述了如何使用表单数据

您最有可能使用 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)