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

时间:2022-08-29 20:22:18

I have a Django template that looks something like this:

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

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

The problem is it comes back empty. If I switch to this:

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

{% if thing == 'foo' or thing == 'bar' %}
    Some HTML here
{% else %}
    Some other HTML
{% endif %}

it works fine. Is there some reason you can't use x in list in Django templates?

它工作正常。有没有什么理由你不能在Django模板中使用x列表?

1 个解决方案

#1


8  

You can. But you can't use a list literal in templates. Either generate the list in the view, or avoid using if ... in ....

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

#1


8  

You can. But you can't use a list literal in templates. Either generate the list in the view, or avoid using if ... in ....

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