Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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) ) }}">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading