小编典典

比较jinja2模板中的两个变量

flask

给定我有两个变量{{ profile }},它们的值分别为“ test”和{{ element.author }}“ test”。在jinja2中,当我尝试使用if比较它们时,没有任何显示。我做如下比较:

{% if profile == element.author %}
{{ profile }} and {{ element.author }} are same
{% else %}
{{ profile }} and {{ element.author }} are **not** same
{% endif %}

我得到的输出test and test are not same怎么了,我该如何比较?


阅读 1287

收藏
2020-04-07

共1个答案

小编典典

只需使用诸如{{ var|string() }}或https://stackoverflow.com/a/19993378/1232796的过滤器{{ var|int() }}

在你的情况下,你想做

{% if profile|string() == element.author|string() %}
{{ profile }} and {{ element.author }} are same
{% else %}
{{ profile }} and {{ element.author }} are **not** same
{% endif %}
2020-04-07