Search code examples
pythonhtmllistflaskjinja2

How to iterate a list from flash message in jinja template in python-flask


I want to iterate a list passed in jinja template using flask flash

Python :

@app.route("/result")
def test():
    my_list = ["item1","item2","item3","item4"]
    flash(my_list)
    return render_template("temp.html")

HTML :

{% with items=get_flashed_messages() %}
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endwith %}

Result :

 - ['item1', 'item2', 'item3', 'item4']

Expected:

 - item1
 - item2
 - item3
 - item4

How I can achieve my expected result using flash function, Please let me know, I will be thankful to you.


Solution

  • Flash each "item" individually. get_flashed_messages() returns a list of the messages you've passed from your server.

    @app.route("/result")
    def test():
        flash("item1")
        flash("item2")
        flash("item3")
        flash("item4")
        return render_template("temp.html")