Post an array of objects from an HTML form using Flask?

This question already has an answer here:

  • Convert form data to JavaScript object with jQuery 50 answers
  • jQuery posting JSON 3 answers
  • JavaScript post request like a form submit 26 answers
  • How to get POSTed json in Flask? 4 answers
  • Return JSON response from Flask view 8 answers

  • You can access the form data as json using form.data
  • For eg

    Consider the form defined as below,

    class GeneralForm(FlaskForm):
        boolean_val = BooleanField('Boolean')
        a_float = FloatField('Severity')
        submit = SubmitField('Submit')
    

    In the app route,

    @app.route('/wtforms', methods=['GET', 'POST'])
    def debug_wtforms():
        form = GeneralForm()
        if request.method == 'POST' and form.validate_on_submit():
            print(form.data) # Form data as a dict
        return render_template('index1.html', form=form)
    
  • If you have simply defined the form directly in the html template, you can access the form data using, request.form.to_dict(flat=False)
  • I hope this helps.

    链接地址: http://www.djcxy.com/p/21928.html

    上一篇: 防止页面内的按钮在点击时刷新页面

    下一篇: 使用Flask从HTML表单发布一个对象数组?