在 Flask html 页面中检查变量条件
Checking a variable condition in Flask html page
我有一个 Flask 应用程序接收 json data.This 是 views.py 中定义的 json 格式。
Values = [
{
'Count':0,
'RPM':0,
'ECT':0
},
{
'Count':1,
'RPM':1,
'ECT':1
}
]
每次更新 json 数据也作为参数传递给 html
@app.route("/members")
def members():
return render_template("members.html",VALS=Values)
在 html 页面中,json 数据是这样处理的
{% for VAL in VALS %}
{% if (VAL['ECT'] > 251) %}
<h1> -> RPM:,ECT:{{VAL.ECT}} <button type="button" class="btn btn-danger btn-sm">High</button> </h1>
{% else %}
<p> {{VAL.Count}} -> RPM:{{VAL.RPM}},ECT:{{VAL.ECT}} <button type="button" class="btn btn-success btn-md">Normal</button> </p>
{% endif %}
{% endfor %}
我在 if.The 条件
中检查条件时遇到问题
{% if (VAL['ECT'] > 251) %}
无效。我该如何解决?
Jinja2 语法中不需要括号。
{% if VAL.get('ECT') > 251 %}
<!-- do stuff -->
{% endif %}
甚至{% if VAL.ECT > 251 %}
.
如果您从视图中将字典传递到模板,该格式应该足够了。但是,如果您传入 JSON,所有内容都被扁平化为字符串,因此您特别需要将值过滤为 int:
{% if VAL.ECT|int > 251 %}<!-- do stuff -->{% endif %}
找到答案
{% if (VAL.get('ECT')|int > 251) %}
这个 work.Need 将其转换为 int :)
我有一个 Flask 应用程序接收 json data.This 是 views.py 中定义的 json 格式。
Values = [
{
'Count':0,
'RPM':0,
'ECT':0
},
{
'Count':1,
'RPM':1,
'ECT':1
}
]
每次更新 json 数据也作为参数传递给 html
@app.route("/members")
def members():
return render_template("members.html",VALS=Values)
在 html 页面中,json 数据是这样处理的
{% for VAL in VALS %}
{% if (VAL['ECT'] > 251) %}
<h1> -> RPM:,ECT:{{VAL.ECT}} <button type="button" class="btn btn-danger btn-sm">High</button> </h1>
{% else %}
<p> {{VAL.Count}} -> RPM:{{VAL.RPM}},ECT:{{VAL.ECT}} <button type="button" class="btn btn-success btn-md">Normal</button> </p>
{% endif %}
{% endfor %}
我在 if.The 条件
中检查条件时遇到问题{% if (VAL['ECT'] > 251) %}
无效。我该如何解决?
Jinja2 语法中不需要括号。
{% if VAL.get('ECT') > 251 %}
<!-- do stuff -->
{% endif %}
甚至{% if VAL.ECT > 251 %}
.
如果您从视图中将字典传递到模板,该格式应该足够了。但是,如果您传入 JSON,所有内容都被扁平化为字符串,因此您特别需要将值过滤为 int:
{% if VAL.ECT|int > 251 %}<!-- do stuff -->{% endif %}
找到答案
{% if (VAL.get('ECT')|int > 251) %}
这个 work.Need 将其转换为 int :)