-1

I want to server images from a folder that is a subfolder of 'static' in my Python 3.7 Flask project. I render the template using render_template('gallery.html', data=data) where data is a list of file paths. Gallery.html is a 3xn grid of pictures.

I tried <img src="{{ url_for({{data[0]}}) }}"> but that doesn't do anything. Just using {{data[0]}} in the template displays the filepath, though.

Something that also didn't work:

def gallery():
   data = get_files() # returns filenames in a list
   render_template('gallery.html', data=data)

Template:

<img src="{{ url_for('static', filename='path/to/folder/{{data[0]}}' }}" >

How do I build the filepath and use them in a html <img> attribute with url_for(), so that my images are loaded correctly?

davidism
  • 121,510
  • 29
  • 395
  • 339
CMorgan
  • 645
  • 2
  • 11
  • 33

1 Answers1

0

The correct solution for me was:

{% for image in data %}
    <img src="{{ url_for('static', filename='images/gallery/' + image) }}" alt="">
{% endfor %}  

The variable doesn't need any brackets or quotes.

CMorgan
  • 645
  • 2
  • 11
  • 33