简体   繁体   中英

Flask-wtforms Redirect on Form Submission and Pass Variables

I am struggling to figure out why I am unable to pass variables to my redirected url. I have attempted the suggestions in the following questions, but for some reason they are not working.

redirect while passing arguments

How can I pass arguments into redirect(url_for()) of Flask?

The only thing I can think of is that it is because I am using flask-wtforms to validate the form before the redirect. I haven't seen much in way of answers/suggestions for this scenario. So here I am.

    @app.route('/quiz/', methods=['GET', 'POST'])
    def quiz():
        form = Quiz()
        if form.validate_on_submit():
            q1_answer = 'North'
            return redirect(url_for('result', q1_answer=q1_answer))
        return render_template('quiz.html', form=form)

    @app.route('/result/', methods=['GET', 'POST'])
    def result():
        q1_correct_answer = request.args.get('q1_answer')
        return render_template('result.html', q1_correct_answer=q1_correct_answer)

The redirect works the way it should. The 'result' url and its corresponding template are rendered. I just can't get the variable (q1_answer) to pass. My template always returns a value of None. Any help would be greatly appreciated.

I think you use it

@app.route('/quiz/', methods=['GET', 'POST'])

def quiz():

form = Quiz()
if request.method == "POST" and form.validate_on_submit():
    q1_answer = 'North'
    return redirect(url_for('result', q1_answer=q1_answer))
return render_template('quiz.html', form=form)

@app.route('/result/', methods=['GET', 'POST'])

def result(q1_answer):

return render_template('result.html', q1_correct_answer=q1_answer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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