如何在Django模板中比较var与整数(0)?

时间:2017-01-06 02:31:59

标签: html django django-models django-templates

我无法想出这一个,这让我疯了!似乎很简单但我必须遗漏一些东西。我试图将计数(整数)与0进行比较,如果计数值为0,则使用no​​ty打印消息。这是我的代码:

var count = 0;
{% for inf in prospect.prospect_industries.all %}
    {% if inf.is_interested %}
        count = count + 1;
    {% endif %}
{% endfor %}

displayNotification(count + " " + zero); //I see 0 0 in the message
{% if count == 0 %}
    displayNotification("Count is 0!!!!");
{% endif %}

我错过了什么?我尝试使用“0”| add:0来转换计数变量,但没有任何作用,它只是永远不会进入if if checking,如果count == 0。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

count是您代码中的Javascript变量,因此请使用Javascript if语句:

if (count === 0) {
    displayNotification("Count is 0!!!!");
}

您只能将{% if %}与从您的观点传递到模板上下文的变量(或user等所有模板中提供的变量等)一起使用。