c00p3r.web c00p3r.web - 1 year ago 81
Python Question

Flask: Flash function ignoring the 'category' argument

So I write the controller:

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 }}
{%- endfor -%}
{%- endif -%}
{%- endwith %}

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

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

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

Answer Source

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')

def index():
    flash('Hello world!', category='success')
    return render_template('index.html')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download