Home > database >  Problem using HTML5 For Loop with flask and Python
Problem using HTML5 For Loop with flask and Python

Time:11-13

I am trying to iterate through a list of tuples in my HTML code, and then use the first item in the tuple, containing the path of a image, to create a new image. The path to the image is 100% correct and when I plug in the path manually the image displays perfectly however when I use the code below it doesn't display. Are there any workarounds for this?

The list I am passing looks something like this

[('assets/photographs/airplane.jpeg', 'airplane', 'iPhone 12 Pro', '1/3861', '1.6', '32', '21 mm'), ('assets/photographs/brooklyn.JPG', 'brooklyn', 'E-M10 Mark III', '1/100', '10.0', '100', '48 mm'), ('assets/photographs/brooklyn2.JPG', 'brooklyn2', 'E-M10 Mark III', '2.0', '4.0', '200', '19 mm'),...... ]

@views.route("/photography")
def photog():
    return render_template("photog.html", data=list)
{% for item in list %}
    <p>{{ item[0] }}</p>
    <img src="{{ url_for('static', filename='{{ item[0] }}')}}">
{% endfor %}

CodePudding user response:

Figured it out, to correct way to do this would be

<img src="{{ url_for('static', filename=item[0])}}">

CodePudding user response:

You've passed 'data' as variable name but used 'list' in your HTML file. You've also placed item[0] into curly brackets, which is no need to do bc you've already opened it once after src attribute. So your code should looks like this:

{% for item in data %}
    <p>{{ item[0] }}</p>
    <img src="{{ url_for('static', filename=item[0])}}">
{% endfor %}

Hope it works.

  • Related