'if' jinja2 模板中的语句
'if' statement in jinja2 template
我正在尝试在 jinja template 中编写一个 if 语句:
{% for key in data %}
{% if key is 'priority' %}
<p>('Priority: ' + str(data[key])</p>
{% endif %}
{% endfor %}
我要在 Python 中翻译的语句是:
if key == priority:
print(print('Priority: ' + str(data[key]))
这是我遇到的错误:
TemplateSyntaxError: expected token 'name', got 'string'
为什么循环?
你可以简单地这样做:
{% if 'priority' in data %}
<p>Priority: {{ data['priority'] }}</p>
{% endif %}
当您最初进行字符串比较时,您应该使用 ==
。
我们需要记住 {% endif %}
在 {% else %}
之后。
这是一个例子:
{% if someTest %}
<p> Something is True </p>
{% else %}
<p> Something is False </p>
{% endif %}
我正在尝试在 jinja template 中编写一个 if 语句:
{% for key in data %}
{% if key is 'priority' %}
<p>('Priority: ' + str(data[key])</p>
{% endif %}
{% endfor %}
我要在 Python 中翻译的语句是:
if key == priority:
print(print('Priority: ' + str(data[key]))
这是我遇到的错误:
TemplateSyntaxError: expected token 'name', got 'string'
为什么循环?
你可以简单地这样做:
{% if 'priority' in data %}
<p>Priority: {{ data['priority'] }}</p>
{% endif %}
当您最初进行字符串比较时,您应该使用 ==
。
我们需要记住 {% endif %}
在 {% else %}
之后。
这是一个例子:
{% if someTest %}
<p> Something is True </p>
{% else %}
<p> Something is False </p>
{% endif %}