Dakkar
Dakkar

Reputation: 5992

Flask: Using WTForm on another Page than the one rendering the form itself

I have the following setup:

The rendering page:

@app.route('/one', methods=['GET'])
def one():
    form = AnyForm()
    return render_template(
        'form.html', form=form
    )

The template (stripped to the basic):

{{ wtf.quick_form(form, action=url_for('two')) }}

The route/function including the logic

@app.route('/two', methods=['POST'])
def two(form):
    print('FORMDATA: ' + form)
    return redirect(url_for('one'))

The error is:

TypeError: two() missing 1 required positional argument: 'form'

How do I handle this?

Upvotes: 0

Views: 43

Answers (1)

DarkSuniuM
DarkSuniuM

Reputation: 2592

You have to define the form on functuin two and populate it from post request that the function recieves, this way you dont need to get any argument

try this:

@app.route('/two', methods=['POST'])
def two():
    form = AnyForm(request.form)
    print('FORMDATA: ' + form)
    return redirect(url_for('one'))

and the way you tried, you have to give your url_for function a form argument like this

{{ wtf.quick_form(form, action=url_for('two', form=form)) }}

Im not sure if it works

Upvotes: 1

Related Questions