当前位置:优学网  >  在线题库

Jinja2模板未检测到我的python变量

发表时间:2022-07-31 02:10:36 阅读:464

我试图在jinja2模板中使用一个简单的if语句:

测试.html:

{% if typeEntry %}
   *insert code A here*       
{% else %}
   *insert code B here*
{% endif %}

测试.py:

@app.route('/')
def testing():
    typeEntry = "new"
    return render_template("testing.html")

尽管typeEntry变量是在我的python代码中定义的,但代码A永远不会只显示代码B,我不知道为什么.

🎖️ 优质答案
  • 在本例中,您需要在`render_template 8215;函数调用中传递所有要注入模板的变量:

    @app.route('/')
    def testing():
        typeEntry = "new"
        return render_template("testing.html", typeEntry=typeEntry)
    
  • You need to pass the variable to render_template():

    return render_template("testing.html", typeEntry=typeEntry)
  • 相关问题