use variables inside image src flask

I am trying to load an image like so:

<img src="{{ url_for('static',filename='output/{{ file_name }}.png') }}">

and this is how I send the file_name variable:

return render_template("somefile.html", file_name=filename)

filename is a variable inside my python code.
I keep getting 404 errors even though my image is inside my static folder inside the ‘output’ folder. any ideas?

error message:

GET /static/output/%7B%20file_name%20%7D HTTP/1.1" 404 –

it seems like the string formatting fails

>Solution :

Definitely a string formatting error

Do this instead

<img src="{{ url_for('static',filename='output/{}.png'.format(file_name) ) }}">

Leave a Reply