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)