检查Flask html页面中的可变条件
我有一个Flask应用程序,它接收json数据。这是views.py中定义的json格式检查Flask html页面中的可变条件,flask,jinja2,Flask,Jinja2,我有一个Flask应用程序,它接收json数据。这是views.py中定义的json格式 Values = [ { 'Count':0, 'RPM':0, 'ECT':0 }, { 'Count':1, 'RPM':1, 'ECT':1 } ] 每个更新的json数据也作为参数传递给html @app.route("/members") def members():
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 %}
它不起作用。如何解决这个问题?在Jinja2语法中不需要括号
{% if VAL.get('ECT') > 251 %}
<!-- do stuff -->
{% endif %}
找到了答案
{% if (VAL.get('ECT')|int > 251) %}
这一个可以工作。需要将其转换为int:)我尝试了两种解决方案,但如果我们使用VAL.ECT,它将不起作用。它将是一个字符串值,与int值进行比较也会起作用吗?啊,当然-如果您将值作为Python dict传递到模板中,它会工作,但JSON被压扁为字符串。正如你在回答中所显示的那样,你需要过滤器。
{% if VAL.ECT|int > 251 %}<!-- do stuff -->{% endif %}
{% if (VAL.get('ECT')|int > 251) %}