Search code examples
pythonjinja2

Jinja2 template is not detecting my python variable


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.


Solution

  • 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)