Search code examples
pythonflaskjinja2

Flask/Jinja2 error Encountered unknown tag 'endblock'


I'm starting to use Flask and I find myself standing on this next mistake:

jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endblock'.

I don't know exactly where the error is, I've already redo the code but I still have the same problem! Here's the code:

app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello, World!"

@app.route("/page1")
def page1():
    return render_template("page1.html")

@app.route("/page2")
def more():
    return render_template("page2.html")

layout.html

<html>
    <head>
        <title> My website </title>
    </head>
    <body>
        <h1> {$ block heading %}{% endblock heading %}} </h1>

        {% block body %}
        {% endblock body %}
    </body>
</html>

page1.html

{% extends "layout.html" %}

{% block heading %}
    First Page
{% endblock heading %}

{% block body %}
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </p>

    <a href="{{ url_for('more') }}"> Leia Mais. </a>
{% endblock body %}

Solution

  • Use this:

    {% endblock %}
    

    In place of the specific endblock things you're using.

    Oh, I also see you're doing this:

    {$ block heading %}{% endblock heading %}}
    

    There are three problems here:

    1. you have a $ symbol, and
    2. you have two end }
    3. as mentioned before, the endblock should just be this: {% endblock %}