Flask,从复选框中获取值并将结果传递给另一个模板(Flask, get value from checkbox and pass the result to another template)

编程入门 行业动态 更新时间:2024-10-22 17:38:46
Flask,从复选框中获取值并将结果传递给另一个模板(Flask, get value from checkbox and pass the result to another template)

我正在使用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

本文发布于:2023-08-07 21:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   框中   复选   模板   Flask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!