Dynamically show url_for image in html file flask

0

Issue

I want to display an image of an item whose name is saved as 1.jpg where 1 is item id.
I am trying to show it using url_for but the image is not showing.

<img class="card-img-top" src="{{url_for('static',filename='img/uploads/item.id.jpg')}}" alt="img">

Is there a way i can dynamically insert item.id value in url_for?

Solution

The below sample should work:

{% for item in items %}
    <img class="card-img-top" src="{{url_for('static',filename='img/uploads/' + item.id + '.jpg')}}" alt="img">
{% endfor %}

[ if id is integer then convert to string like item.id|string ]

Answered By – Akash Unni

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More