flask - 检查 Flask html 页面中的变量条件

标签 flask jinja2

我有一个接收json数据的Flask应用程序。这是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) %}

不工作。我该如何解决这个问题?

最佳答案

找到答案

{% if (VAL.get('ECT')|int > 251) %}

这个可以工作。需要将其转换为 int :)

关于flask - 检查 Flask html 页面中的变量条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31770690/

相关文章:

jinja2 - 如果传入模板中不存在的变量,则会在jinja中引发异常

flask - 如何从蓝图模板中访问应用程序名称?

python - 在使用 Flask url : (35) schannel: next InitializeSecurityContext failed 的 REST API 的curl URL 期间出现问题

python - 在 SQLAlchemy 中获取传感器的每种类型的最新读取值

reactjs - 如何从关系模型中获取其他字段?

sorting - 根据Ansible中的特定值对dict进行排序

python - flask-login 中不存在 next_is_valid()?

python - 将数据传递到 jinja 模板中的 Bootstrap 模式(其中一个来自 for 循环内部)

python - 多平台代码生成

python - Jinja2 是否支持嵌套的 if 语句?