Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect while passing arguments

Tags:

python

flask

In flask, I can do this:

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

And if foo.html contains {{ messages['main'] }}, the page will show hello. But what if there's a route that leads to foo:

@app.route("/foo") def do_foo():     # do some logic here     return render_template("foo.html") 

In this case, the only way to get to foo.html, if I want that logic to happen anyway, is through a redirect:

@app.route("/baz") def do_baz():     if some_condition:         return render_template("baz.html")     else:         return redirect("/foo", messages={"main":"Condition failed on page baz"})          # above produces TypeError: redirect() got an unexpected keyword argument 'messages' 

So, how can I get that messages variable to be passed to the foo route, so that I don't have to just rewrite the same logic code that that route computes before loading it up?

like image 457
limp_chimp Avatar asked Jun 12 '13 03:06

limp_chimp


People also ask

How do you pass an argument in redirect?

To pass arguments into redirect(url_for()) of Flask, we define the destination route to get the request parameters. Then we can call url_for with the parameters. to add the /found/<email>/<list_of_objects> route that maps to the found function. In it, we get the the URL parameters from the found function's parameters.

What is the difference between Render_template and redirect?

redirect returns a 302 header to the browser, with its Location header as the URL for the index function. render_template returns a 200, with the index. html template returned as the content at that URL.

How do you redirect in Flask?

Flask – Redirect & ErrorsFlask class has a redirect() function. When called, it returns a response object and redirects the user to another target location with specified status code. location parameter is the URL where response should be redirected. statuscode sent to browser's header, defaults to 302.


2 Answers

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:

from flask import session, url_for  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.

like image 130
Tommi Komulainen Avatar answered Oct 02 '22 16:10

Tommi Komulainen


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!

like image 28
Nick Woodhams Avatar answered Oct 02 '22 16:10

Nick Woodhams