Flask,如何将所有变量传递到模板Jinja中

时间:2020-07-24 10:31:19

标签: python templates flask jinja2

我想在不知道变量名的情况下检索所有变量名。

好吧, 我有两条路线使用相同的模板。

* app.py

@app.route("/toto")
def toto():
   toto = [1,2,3]
   tata = [1,2,3]
   titi = [1,2,3]
   return render_template("exemple.html", toto=toto, tata=tata, titi=titi)

@app.route("/tata")
def toto():
   tutu= [1,2,3]
   tyty = [1,2,3]
   titi = [1,2,3]
   return render_template("exemple.html", toto=toto, tyty=tyty , titi=titi)

* exemple.html

{% set var = ALL_VARIABLE %}
{% for v in var %}
    {{ v }}
{{ endfor }}

我重新发布此答案,因为管理员认为是更好的主意。

1 个答案:

答案 0 :(得分:0)

我会将3个变量添加到这样的列表中

tutu= [1,2,3]
tyty = [1,2,3]
titi = [1,2,3]

var = []
var.append(tutu)
var.append(titi)
var.append(tyty)
return render_template("exemple.html", var = var)

并在您的模板中:

{% for v in var %}
  {{ v }}
{{ endfor }}
相关问题