【问题标题】:Get data from WTForms form从 WTForms 表单获取数据
【发布时间】:2017-04-16 10:02:31
【问题描述】:

提交后如何从 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>

【问题讨论】:

    标签: python flask wtforms


    【解决方案1】:

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

    the_email = form.email.data
    

    getting started doc 中描述了处理表单数据。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-08
        • 2017-10-23
        • 2015-11-24
        • 2011-07-09
        • 1970-01-01
        • 2021-05-03
        • 2018-03-20
        相关资源
        最近更新 更多