gpt4 book ai didi

python - Django - 'if x in list' 的模板不起作用

转载 作者:行者123 更新时间:2023-12-04 14:15:14 24 4
gpt4 key购买 nike

我有一个 Django 模板,看起来像这样:

{% if thing in ['foo', 'bar'] %}
Some HTML here
{% else %}
Some other HTML
{% endif %}

问题是它返回空。如果我切换到这个:
{% if thing == 'foo' or thing == 'bar' %}
Some HTML here
{% else %}
Some other HTML
{% endif %}

它工作正常。是否有某些原因您不能使用 x in list在 Django 模板中?

最佳答案

你可以。但是您不能在模板中使用列表文字。要么在 View 中生成列表,要么避免使用 if ... in ... .

关于python - Django - 'if x in list' 的模板不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14328525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com