【问题标题】:Substring at flask template烧瓶模板处的子字符串
【发布时间】:2015-07-29 03:49:21
【问题描述】:

我有一个循环来显示烧瓶模板中的列表内容,但我不想显示元素的第一个字符,这样在 python 中有效,但在烧瓶中无效

{%for file in files%}
        {% f= file['path'] %}
        <p> {{ f[1:] }}</p>
{% endfor %}

我收到这个错误

TemplateSyntaxError: Encountered unknown tag 'f'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.

【问题讨论】:

    标签: python flask


    【解决方案1】:

    this question 的重复。

    您必须使用 {% set %} 模板标签在 jinja2 模板中分配变量:

    {% for file in files %}
        {% set f = file['path'] %}
        <p>{{ f[1:] }}</p>
    {% endfor %}
    

    【讨论】:

    • 话虽如此,Doobeh 的想法是正确的:只取 dict 值的一部分就可以完全避免这个问题。
    【解决方案2】:

    如果您希望以这种方式使用变量,则需要 set 变量。 (Documentation)。

    也就是说,您应该能够在您的 for 循环中执行 {{ file['path'][1:] }}

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多