【问题标题】:Problem using HTML5 For Loop with flask and Python使用带有烧瓶和 Python 的 HTML5 For Loop 出现问题
【发布时间】:2021-12-24 12:01:50
【问题描述】:

我正在尝试遍历我的 HTML 代码中的元组列表,然后使用元组中包含图像路径的第一项来创建新图像。图像的路径是 100% 正确的,当我手动插入路径时,图像会完美显示,但是当我使用下面的代码时,它不会显示。有什么解决方法吗?

我传递的列表看起来像这样

[('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 %}

【问题讨论】:

    标签: python html python-3.x flask


    【解决方案1】:

    想通了,正确的方法是

    &lt;img src="{{ url_for('static', filename=item[0])}}"&gt;

    【讨论】:

      【解决方案2】:

      您已将“数据”作为变量名传递,但在 HTML 文件中使用了“列表”。 您还将item[0] 放入大括号中,无需这样做,因为您已经在src 属性之后打开了它一次。 所以你的代码应该是这样的:

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

      希望它有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-04
        • 1970-01-01
        • 2015-06-21
        • 1970-01-01
        • 2012-10-30
        • 2016-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多