3

I've been working on flask and I came across flash. It displays a message on page if flash was called on the server side. However, I'd like to display the contents get_flashed_messages() in an alertbox. I tried to make JS function and pass {{message}} but the loop prints the call i.e. takes the call as a string.

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

IS there any way to go about it?

r-m-n
  • 14,192
  • 4
  • 69
  • 68
mini.1601
  • 157
  • 1
  • 2
  • 12
  • I need to return an alertbox. AFAIK flask can only return http markup at most. I need to call a JS function, not a python function. – mini.1601 Nov 07 '15 at 07:27

1 Answers1

15

You can create JS variable containing messages

{% with messages = get_flashed_messages() %}
  {% if messages %}
    <script>
      var messages = {{ messages | safe }};
      for (var i=0; i<messages.length; i++) {
        alert(messages[i]);
      }
    </script>
  {% endif %}
{% endwith %}
r-m-n
  • 14,192
  • 4
  • 69
  • 68