redirect while passing arguments

后端 未结 4 753
情深已故
情深已故 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.

    0 讨论(0)
  • 2020-11-27 15:49

    I'm a little confused. "foo.html" is just the name of your template. There's no inherent relationship between the route name "foo" and the template name "foo.html".

    To achieve the goal of not rewriting logic code for two different routes, I would just define a function and call that for both routes. I wouldn't use redirect because that actually redirects the client/browser which requires them to load two pages instead of one just to save you some coding time - which seems mean :-P

    So maybe:

    def super_cool_logic():
        # execute common code here
    
    @app.route("/foo")
    def do_foo():
        # do some logic here
        super_cool_logic()
        return render_template("foo.html")
    
    @app.route("/baz")
    def do_baz():
        if some_condition:
            return render_template("baz.html")
        else:
            super_cool_logic()
            return render_template("foo.html", messages={"main":"Condition failed on page baz"})
    

    I feel like I'm missing something though and there's a better way to achieve what you're trying to do (I'm not really sure what you're trying to do)

    0 讨论(0)
  • 2020-11-27 15:54

    I want to mention that url_for cannot be used until it is added to the flask imports!

    I spent an hour trying to find out why it would not work as no one mentioned that in several websites!!!

    Add url_for to the list of imports like this:

    from flask import Flask, render_template, request, redirect, session, url_for
    
    0 讨论(0)
  • 2020-11-27 15:56

    I found that none of the answers here applied to my specific use case, so I thought I would share my solution.

    I was looking to redirect an unauthentciated user to public version of an app page with any possible URL params. Example:

    /app/4903294/my-great-car?email=coolguy%40gmail.com to

    /public/4903294/my-great-car?email=coolguy%40gmail.com

    Here's the solution that worked for me.

    return redirect(url_for('app.vehicle', vid=vid, year_make_model=year_make_model, **request.args))
    

    Hope this helps someone!

    0 讨论(0)
提交回复
热议问题