0

我分配 'result':True 并将其传递给模板。

对于模板文件,我写

{{ result }}
{% if result == True %}
    this also True
{% endif %}
{% if result == 1 %}
    this is 1
{% endif %}
{% if result == true %}
    this is true
{% endif %}

结果是,

' 真的这是 1 '

我认为 'Trthis 也是真的

4

1 回答 1

1

正如@PepperoniPizza 在上面的评论中提到的那样,在 Python 中不建议将值TrueFalse==. 大多数时候,你只需要{% if result %}在你的模板中使用——不管结果是True,1还是其他值而不是评估为True

如果你真的需要这样做{% if result == True %},这将在 Django 1.5 中工作。从发行说明

模板引擎现在将 True、False 和 None 解释为相应的 Python 对象。

于 2012-11-14T15:15:59.177 回答