Search code examples
pythonflaskjinja2web-deployment

Fixing “expected token 'name', got 'integer'” error in python flask with Jinja 2 templating


I really face an error in Flask. When I try to put if statement to insert some elements inside it, a token error saying expected token 'name', got 'integer' arise.

And when I try to delete if statement then the token error disappears and the entire page with its contents is smoothly displayed.

Below are some of my codes.

Page.html

{% for content in contents %}
   <div>
       *some divs here*
       {{content}}
   </div>

{% if content.index is 3 %}
   <div>
        *some contents inside*
   </div>
{% endif %}
{% endfor %}

Solution

  • In your if statement condition, you should use == rather than is.

    is is used to compare against the type rather than the value.