puranjan
puranjan

Reputation: 373

Dynamically show url_for image in html file flask

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?

Upvotes: 0

Views: 1172

Answers (1)

Akash Unni
Akash Unni

Reputation: 406

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 ]

Upvotes: 1

Related Questions