Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Template with 'if x in list' not working

Tags:

python

django

I have a Django template that looks something like this:

{% 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?

like image 397
serverpunk Avatar asked Jan 14 '13 23:01

serverpunk


1 Answers

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

like image 199
Ignacio Vazquez-Abrams Avatar answered Oct 04 '22 18:10

Ignacio Vazquez-Abrams