Skip to main content
 首页 » 编程设计

python之Django 模板 {% if %} : what does it take to be equal

2025年05月04日27三少

这是一段来自 Django 模板的代码:

{% for customer in customers %} 
    {% for div in divs|lookup:customer %} 
        <p>:{{ div.custdiv_id }}: vs. :{{ currentDiv }}: ==> 
            {% if div.custdiv_id == currentDiv %} 
                YES! 
            {% else %} 
                NOPE. 
            {% endif %} 
        </p> 
    {% endfor %} 
{% endfor %} 

这是渲染模板的一些输出:

:1: vs. :1: ==> NOPE. 
:7: vs. :1: ==> NOPE. 
:5: vs. :1: ==> NOPE. 
:9: vs. :1: ==> NOPE. 

为什么 :1: 与 :1: 不打印“YES”?冒号是为了检查它是否不是一些间距问题,它似乎不是; div.custdiv_id 和 currentDiv 在它们各自的模型上来自同一类型——bigint(20)。我不知道这里的问题是什么。

请您参考如下方法:

你为什么不使用 {% ifequal div.custdiv_id currentDiv %}