简体   繁体   English

如何在 flask 模板中对变量执行数学运算

[英]how to perform math operation on a variable into flask template

I just need to perform this我只需要执行此操作

{% i += 1 %}

but I end with this error但我以这个错误结束

jinja2.exceptions.TemplateSyntaxError:
       expected token 'end of statement block', got '+'

problem is to perform a math operation within a loop and every time the variable should change.. not printing it问题是在循环中执行数学运算,每次变量应该改变时..不打印它


edit编辑

I reached this point {% set i = sum([i, 1]) %}我达到了这一点{% set i = sum([i, 1]) %}

but within for loop it prints 111111111 not 123456789 '_'但在 for 循环中它打印 111111111 而不是 123456789 '_'

try to do the following code, just do the math operation before.尝试做下面的代码,只是做之前的数学运算。 it worked for me.它对我有用。

i+=1
return render_template("file.html",i=i)

And write in your html file the following line:并在您的 html 文件中写入以下行:

<p>{{i}}</p>

Jinja math operations does not allow the operator += . Jinja 数学运算不允许运算符+= You can see the available operators here您可以在此处查看可用的运算符

You can do something like你可以做类似的事情

{% set count =  i + 1 %}

Given i is already a defined variable.鉴于i已经是一个定义的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM