c00p3r.web c00p3r.web - 1 month ago 11
Python Question

Flask: Flash function ignoring the 'category' argument

So I write the controller:

@app.route('/')
def index():
flash('Hello world!', 'success')
return render_template('index.html')


then in my template I output the flash messages like this:

{%- with messages = get_flashed_messages(with_categories=true) -%}
{%- if messages -%}
<ul class="flashes unstyled">
{%- for category, message in messages -%}
<li class="alert alert-{{ category }}">
<a class="close" data-dismiss="alert">&times;</a>
{{ message }}
</li>
{%- endfor -%}
</ul>
{%- endif -%}
{%- endwith %}


But the issue is that I ALWAYS get just 'message' category so
<li>
goes with classes
'alert alert-message'
.

I read the docs and as to me I did everything right, but
'flash'
function ignoring the second argument and always uses the default value
'message'
(instead of given by me 'success').

I wonder if anyone had that issue and know how to handle it?

Answer

Edit: Based on other comments and testing the kwarg is unnecessary.

Based on the docs at http://flask.pocoo.org/docs/api/#message-flashing it appears you need to use this format. flash(message, category='message')

@app.route('/')
def index():
    flash('Hello world!', category='success')
    return render_template('index.html')