我正在使用Weasyprint,在Flask Web App中显示一些jinja模板。 我有这个json。
value=["1","2","3","4"]我想在if语句中将'value'传递给另一个jinja模板。
{% if (value|int =["1", "2", "3", "4"]) %} {% include 'pages/page1.html' %} {% else %} {% include 'pages/page2.html' %} {% endif %}但是这显示了错误,
TemplateSyntaxError: expected token ')', got '='我以为我必须将json转换为int才能使其工作。
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.
最满意答案
Jinja2 int过滤器将在列表中失败,因为它将试图将其转换为单个整数值。 因此它将返回0 。 另外=是一个赋值运算符, ==是一个比较器。 试试这个来得到预期的结果:
{% if value|join("|") == "1|2|3|4|5" %} {% include 'pages/page1.html' %} {% else %} {% include 'pages/page2.html' %} {% endif %}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 %}更多推荐
json,jinja,int,电脑培训,计算机培训,IT培训"/> <meta name="description&qu
发布评论