问题描述
我在我的模板中有以下代码,它应该将 watchinstance.shift
的值(可以是DAY或NIGHT)与文字字符串DAY。比较总是失败。 {%for watchinstance in watchinstance_list%}
{%if watchinstance.shift == DAY%}
< p> shift是DAY< / p>
{%endif%}
{%endfor%}
使用 ifequal
不起作用:
{%for watchinstance in watchinstance_list%}
{%ifequal watchinstance.shiftDAY%}
< p> shift is DAY< / p>
{%endifequal%}
{%endfor%}
调用 {{watchinstance.shift}}
按预期工作:
%watchinstance_list%}
{{watchinstance.shift}}
{%endfor%}
返回DAY和NIGHT。
我检查了 watchinstance.shift
是否返回任何额外的字符,而且它看起来不像...它还有什么可以在这里丢失?
几种可能性:
-
.shift字符串有额外的空格。使用它来仔细检查:
{%for watchinstance in watchinstance_list%}
X {{watchinstance.shift} } X
{%endfor%}
-
.shift属性不是一个字符串,但是一个字符串到DAY或NIGHT的对象。在这种情况下,
{{watchinstance.shift}}
中的变量替换与字符串看起来一样,但在{%ifequal watchinstance .shiftDAY%}
将失败。
I have the following code in my template that supposed to compare the value of watchinstance.shift
, which can be either "DAY" or "NIGHT", to a literal string "DAY". The comparisson always fails.
{% for watchinstance in watchinstance_list %}
{% if watchinstance.shift == "DAY" %}
<p>shift is DAY</p>
{% endif %}
{% endfor %}
Using ifequal
doesn't work either:
{% for watchinstance in watchinstance_list %}
{% ifequal watchinstance.shift "DAY" %}
<p>shift is DAY</p>
{% endifequal %}
{% endfor %}
However, just calling {{ watchinstance.shift }}
works as expected:
{% for watchinstance in watchinstance_list %}
{{ watchinstance.shift }}
{% endfor %}
returns DAYs and NIGHTs.
I checked whether watchinstance.shift
returns any extra characters, and it doesn't look like it does... What else can I be missing here?
A couple of possibilities:
The .shift string has extra whitespace. Use this to double-check:
{% for watchinstance in watchinstance_list %} X{{ watchinstance.shift }}X {% endfor %}
The .shift attribute isn't a string, but an object that stringifies to "DAY" or "NIGHT". In that case, the variable substitution in
{{ watchinstance.shift }}
would look the same as a string, but the comparison in{% ifequal watchinstance.shift "DAY" %}
would fail.
这篇关于Django模板变量值对字符串文字比较失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!