简体   繁体   English

Flask-wtforms 重定向表单提交和传递变量

[英]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.我正在努力弄清楚为什么我无法将变量传递给重定向的 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? 如何将参数传递到 Flask 的 redirect(url_for()) 中?

The only thing I can think of is that it is because I am using flask-wtforms to validate the form before the redirect.我唯一能想到的是,这是因为我在重定向之前使用了flask-wtforms 来验证表单。 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. 'result' url 及其相应的模板被呈现。 I just can't get the variable (q1_answer) to pass.我只是无法让变量 (q1_answer) 通过。 My template always returns a value of None.我的模板总是返回 None 值。 Any help would be greatly appreciated.任何帮助将不胜感激。

I think you use it我想你用它

@app.route('/quiz/', methods=['GET', 'POST']) @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']) @app.route('/result/', methods=['GET', 'POST'])

def result(q1_answer):定义结果(q1_answer):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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