Message Flashing

    So here is a full example:

    And here is the template which does the magic:

    1. <!doctype html>
    2. {% with messages = get_flashed_messages() %}
    3. {% if messages %}
    4. <ul class=flashes>
    5. {% for message in messages %}
    6. {% endfor %}
    7. </ul>
    8. {% endif %}
    9. {% endwith %}

    Here is the index.html template which inherits from layout.html:

    And here is the template which also inherits fromlayout.html:

    {% extends "layout.html" %}
    {% block body %}
      <h1>Login</h1>
      {% if error %}
        <p class=error><strong>Error:</strong> {{ error }}
      {% endif %}
      <form method=post>
        <dl>
          <dt>Username:
          <dd><input type=text name=username value="{{
              request.form.username }}">
          <dt>Password:
          <dd><input type=password name=password>
        </dl>
        <p><input type=submit value=Login>
      </form>
    {% endblock %}
    

    Changelog

    New in version 0.3.

    It is also possible to provide categories when flashing a message. Thedefault category if nothing is provided is 'message'. Alternativecategories can be used to give the user better feedback. For exampleerror messages could be displayed with a red background.

    To flash a message with a different category, just use the second argumentto the function:

    Inside the template you then have to tell the function to also return thecategories. The loop looks slightly different in that situation then:

    {% with messages = get_flashed_messages(with_categories=true) %}
      {% if messages %}
        <ul class=flashes>
        {% for category, message in messages %}
          <li class="{{ category }}">{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}
    

    Changelog

    New in version 0.9.

    Optionally you can pass a list of categories which filters the results of. This is useful if you wish torender each category in a separate block.