Issue
I have a simple flask
application where I need to show 2 images in a html
page, all the images are stored in a static
folder
While rendering the html
page I am passing image file names as a list and on html
page I am using jinja
logic to show the images
img_lst = [filename1, filename2]
return render_template("index.html", img_lst=img_lst)
I can easily loop over the list and show images using below logic
{% for image in img_lst %}
<img src="{{url_for('static', filename=image)}}">
{% endfor %}
but if I index a particulr file from a list I am getting an error
jinja2.exceptions.UndefinedError: 'img_lst' is undefined
<img src=" {{url_for('static', filename=img_lst[0])}}">
I want to individually access filenames from list instead of looping over
Solution
i don’t see any problem with your code above, but i’m guessing you need to force flask
to reload templates, so jinja2
will take in consideration your variable img_lst
the next reload.
add this config below and let me know if it works:
app.config["TEMPLATES_AUTO_RELOAD"] = True
Update
Your code works and i didn’t get that error, so my guess probably you are working on other view/template and you forgot to pass img_lst
in the context in render function.
Answered By – cizario
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0