Here's the example:
from flask import Flask, flash, get_flashed_messages, redirect, url_for
app = Flask(__name__)
app.secret_key = 'some_secret'
@app.route('/')
def index():
app.logger.debug(get_flashed_messages())
return 'Hello world'
@app.route('/generate/success')
def page_success():
flash('This message will be visible!')
return str(get_flashed_messages())
@app.route('/generate/fail',)
def page_fail():
flash('This message will be lost!')
return redirect(url_for('index'))
if __name__ == "__main__":
app.run()
As you can see flash() fails when using redirect. Tried with SERVER_NAME both set and unset. Using Flask==0.10.1.
Please resolve the issue ASAP since it's a top blocker in my project.
It should work fine. Does your actual project call get_flashed_messages() before redirecting by any chance?
Nope, I don't think so. But what's strange the above example started working out of the blue... I need to investigate it further since my project still is facing the issue. Nonetheless, all ideas are welcome.
EDIT:
Don't know if it's related by in the above example I get explicit requests information wriitten on the terminal (eq. 127.0.0.1 GET / 200) whereas in my project such a debug information is not visible. However stuff gets written when using app.logger. I must have done something nasty in my project...
Here's, simplified to GET handler, code of my project:
class NewNewsView(MethodView):
def get(self):
flash('This message gets lost') # this message gets lost
return redirect(url_for('index'))
class IndexView(MethodView):
def get(self):
return render_template('index.html') # uses get_flashed_message() in template
Okay, I got it. The issue was caused by setting APPLICATION_ROOT which is discouraged in the Wiki so the fault was undoubtedly on my side. Sorry for bothering you. I'm gonna close this issue now.
Encountered this today, removed APPLICATION_ROOT, and problem fixed. Thanks for the update after your fix @robin92, much appreciated.
I am having the same issue and I don't have an APPLICATION_ROOT in my project. After flashing a message, if I return redirect('/ ') the message doesn't appear on the redirected page but if I comment out the redirect and use return render_template("index.html") the message does display.
Setting cache timeout to 0 "fixes" this but this site relies on caching for performance reasons so if there is some way to get message flashing to work on redirect and WITH caching enabled, please let me know. Thanks!!
Most helpful comment
Okay, I got it. The issue was caused by setting
APPLICATION_ROOTwhich is discouraged in the Wiki so the fault was undoubtedly on my side. Sorry for bothering you. I'm gonna close this issue now.