rookiedee rookiedee - 12 days ago 5
JSON Question

Flask, get value from checkbox and pass the result to another template

I am using Weasyprint, to display some jinja templates in a Flask Web App.
I have this json.

value=["1","2","3","4"]


I want to pass 'value' to another jinja template in an if statement.

{% if (value|int =["1", "2", "3", "4"]) %}
{% include 'pages/page1.html' %}
{% else %}
{% include 'pages/page2.html' %}
{% endif %}


But this shows the error,

TemplateSyntaxError: expected token ')', got '='


I thought I had to convert json to int in order to make it work.

Answer

The Jinja2 int filter will fail on your list as it will be trying to cast it to a single integer value. Therefore it will return 0. Also = is an assignment operator and == is a comparator. Try this to get the intended result:

{% if value|join("|") == "1|2|3|4|5"  %}
{% include 'pages/page1.html' %}
{% else %}
{% include 'pages/page2.html' %}
{% endif %}