Alex Dana
Alex Dana

Reputation: 1287

Display image with variable and URL_FOR - Flask

My target is to display an image on a website, with Flask.

The image is in :

/static/img/articles

The image name is : "43.jpg"

Hardcoding it is perfectly working :

<img src="{{ url_for('static', filename='img/articles/43.jpg') }}" alt="no picture">

However, i need the path to be dynamic (the image is 43 today, maybe 17 tomrrow and 58 after tomorrow).

I tried everything, for example this :

last_id_jpg = r"img/article/43.jpg"

return render_template("index3.html",
                        last_id_jpg = last_id_jpg
                       )

with :

<img src="{{ url_for('static', filename=last_id_jpg) }}" alt="no picture">

or :

<img src="{{ url_for('static', filename='last_id_jpg') }}" alt="no picture">

But can't find the path to the designed image (the webpage displays "no picture"). I made some researches and everything seems OK

What is wrong in my code ?

Upvotes: 1

Views: 3488

Answers (1)

bertdida
bertdida

Reputation: 5288

You have a typo on your last_id_jpg. It should be

last_id_jpg = r"img/articles/43.jpg" # article with s

not

last_id_jpg = r"img/article/43.jpg"

Upvotes: 1

Related Questions