Search code examples
pythonhtmlflaskjinja2template-inheritance

jinja2.exceptions.TemplateSyntaxError in flask


I can't get rid of this exception and I have no idea what's wrong. Thanks for everything.

This is my python file

app = Flask(__name__)
@app.route("/login")
def login():
    return render_template("login.html")

And this is my login.html


{% extends = "layout.html" %}


{% block title %}
    Login
{% endblock %}

{% block heading %}Login {% endblock %}

{% block body %}
<h1>Login</h1>
{% endblock %}

And this is layout.html

<!doctype html>
<html>
  <head>
    <title>{% block title %}{% endblock %} - My Webpage</title>

  </head>
  <body>
    <h1>{% block heading %} {% endblock %}</h1>
    {% block body %}
    {% endblock %}
  </body>
</html>

Solution

  • As per Jinja documentation, you cannot put equal to (=) for statements like {% extends %} in your template.

    Your login.html should be like this:

    {% extends "layout.html" %}
    {% block title %}
        Login
    {% endblock %}
    {% block heading %}Login {% endblock %}
    {% block body %}
    <h1>Login</h1>
    {% endblock %}
    

    for more info Check this.