比较jinja2模板中的两个变量

use*_*366 9 python google-app-engine 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 %}
Run Code Online (Sandbox Code Playgroud)

我得到输出test and test are not same什么是错的,我怎么比较?

tgd*_*gdn 14

我有同样的问题,当它们是相同的值时,两个具有整数值的变量不等于相同.

有没有办法以任何方式使这项工作.还尝试使用str()== str()或int()== int(),但始终存在未定义的错误.

UPDATE

找到解决方案: 只需使用过滤器{{ var|string() }}{{ var|int() }} /sf/answers/1399536491/

阅读文档可以在这里找到http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-filters

在你的情况下,你会想做

{% if profile|string() == element.author|string() %}
{{ profile }} and {{ element.author }} are same
{% else %}
{{ profile }} and {{ element.author }} are **not** same
{% endif %}
Run Code Online (Sandbox Code Playgroud)


mji*_*son 2

profileelement.author不是同一类型,或者不相等。但是,当转换为字符串时,它们确实会输出相同的值。您需要正确比较它们或将它们的类型更改为相同。