Lui*_*ito 14 python templates jinja2
我正在尝试在jinja模板中编写if语句
{% for key in data %}
{% if key is 'priority' %}
<p>('Priority: ' + str(data[key])</p>
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我试图在python中翻译的声明是
if key == priority:
print(print('Priority: ' + str(data[key]))
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
TemplateSyntaxError:期望令牌'name',得到'string'
Nic*_*ick 34
为什么循环?
你可以这样做:
{% if 'priority' in data %}
<p>Priority: {{ data['priority'] }}</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
当你最初进行字符串比较时,你应该使用它==.
Mic*_*des 16
我们需要记住,{% endif %}之后是{% else %}.
所以这是一个例子:
{% if someTest %}
<p> Something is True </p>
{% else %}
<p> Something is False </p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68485 次 |
| 最近记录: |