Home > Net >  Jinja2 template is not detecting my python variable
Jinja2 template is not detecting my python variable

Time:07-31

I am trying to use a simple if statement in a jinja2 template as such:

testing.html :

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

testing.py:

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

Even though typeEntry variable is defined in my python code, code A is never shown only code B, I have no idea why.

CodePudding user response:

you need to pass all the variables you want to inject in the template in the render_template function call, in your case:

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

CodePudding user response:

You need to pass the variable to render_template():

return render_template("testing.html", typeEntry=typeEntry)
  • Related