redirect while passing arguments

后端 未结 4 791
情深已故
情深已故 2020-11-27 15:07

In flask, I can do this:

render_template(\"foo.html\", messages={\'main\':\'hello\'})

And if foo.html contains {{ messages[\'main\']

4条回答
  •  悲&欢浪女
    2020-11-27 15:44

    You could pass the messages as explicit URL parameter (appropriately encoded), or store the messages into session (cookie) variable before redirecting and then get the variable before rendering the template. For example:

    def do_baz():
        messages = json.dumps({"main":"Condition failed on page baz"})
        session['messages'] = messages
        return redirect(url_for('.do_foo', messages=messages))
    
    @app.route('/foo')
    def do_foo():
        messages = request.args['messages']  # counterpart for url_for()
        messages = session['messages']       # counterpart for session
        return render_template("foo.html", messages=json.loads(messages))
    

    (encoding the session variable might not be necessary, flask may be handling it for you, but can't recall the details)

    Or you could probably just use Flask Message Flashing if you just need to show simple messages.

提交回复
热议问题