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

Rob*_*Rob 6 python django

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

{% if thing in ['foo', 'bar'] %}
    Some HTML here
{% else %}
    Some other HTML
{% endif %}
Run Code Online (Sandbox Code Playgroud)

问题是它回来了.如果我切换到这个:

{% if thing == 'foo' or thing == 'bar' %}
    Some HTML here
{% else %}
    Some other HTML
{% endif %}
Run Code Online (Sandbox Code Playgroud)

它工作正常.有没有什么理由你不能x in list在Django模板中使用?

Ign*_*ams 9

您可以.但是你不能在模板中使用列表文字.在视图中生成列表,或者避免使用if ... in ....