3

I was wondering if it is possible to pass a value through redirect(url_for()) with out it becoming a GET.

Basic example: I have a home function:

@app.route("/")
def home(msg=None):
    return render_template("home.html", mgs=msg)

how can I pass an message to the home function? In other words can I do this:

@app.route("/logout")
def logout():
    logout_user()
    return render_template('home.html', msg="logged out")

with redirect(url_for())?

this:

@app.route("/logout")
def logout():
    logout_user()
    return redirect(url_for('home', msg="logged out"))

just gives me the url with a get /?msg='logged out'.

I understand that url_for is generating an url based on the name of the function being passed to it. Can you pass a value to that function?

2
  • A gross (but not totally impractical) way would be to use a session variable, but other than that, No you cannot. Commented Aug 2, 2017 at 19:20
  • You can, if the URL is expecting that variable. So if your route was "/logout/:message", that would work, but then your URL would end up as "/logout/logged out" which would be even more horrible. (Although note there's nothing at all gross about using sessions; that's exactly what they're for.) Commented Aug 2, 2017 at 19:23

5 Answers 5

5

What about a flash variable?

flash("logged out")
return redirect(url_for('home'))

Then in template for home:

{% with passed = get_flash_messages() %}
{% if passed %}
    <!-- passed should contain 'logged out' -->
{% endif %}

see also: http://flask.pocoo.org/docs/0.12/patterns/flashing/

Sign up to request clarification or add additional context in comments.

Comments

3

Another way to go is using sessions:

Dont forget to add it to your imports

from flask import Flask, ..., session

In logout definition store the message in the session:

@app.route("/logout")
def logout():
    logout_user()
    session['msg'] = "logged out"
    return redirect(url_for('home'))

In the home function

@app.route("/")
def home(msg=None):
    return render_template("home.html", msg=session['msg'])

In the home template just use it as a usual variable:

<p>Dear user, you have been {{ msg }} </p>

This way you avoid using get and Flask takes care of all

Comments

0

No, I Do not think so, but you can use GET parameters like:

redirect('home?msg=You%20are%20loged%20in')

and instead of using a template to do the work you can use JS

3 Comments

Yeah, I wanted to use the value passed in the template. Seems messy using the GETs. (user can make page say what ever) I guess I'll just used render_template when needed.
I am not following here, What is wrong with the user changing the text that the page says?
nothing wrong, I was just trying to avoid using GETs
0

Did you try this?

msg="logged out"
return redirect(url_for('home'),code=307)

Code 307 is used as "POST"

Comments

0

Just use an f-string in your route direction.

redirect(f'/route/{variable}')

1 Comment

There seems to be a missing closing parenthesis ) in your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.