Question about jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endfor'. Jinja was looking for the following tags: 'endblock'
Solution 1
You have a Typo, in the second for-loop
{ % for post in posts %}
{% include '_post.html' %}
{% endfor %}
It Should be like below:
{% for post in posts %}
{% include '_post.html' %}
{% endfor %}
The Reason: Since there is space after {
before %
Jinja will not recognize it as an endfor tag. thus giving you the error.
Solution 2
People searching for this error message might also like to know that the same error can result if you accidentally use a paren instead of a brace:
(% for ... %}
It seems like a silly mistake, but one that I've made a few times over the years and get stumped for a bit. It's easy to scan the code too quickly and not see the offending paren.
Stephen Tham
Updated on September 01, 2022Comments
-
Stephen Tham almost 2 years
I am trying to build a website with Flask and I have been encountered a
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endfor'. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'
error. When I try to log into my local webpage with localhost:5000 I get the above error.I have read through the traceback error and it seems to be occurring on line 21 of my index.html file.
{% block content %} <h1>Hi, {{ current_user.username }}!</h1> {% if form %} <form action="" method="post"> {{ form.hidden_tag() }} <p> {{ form.post.label }}<br> {{ form.post(cols=32, rows=4) }}<br> {% for error in form.post.errors %} <span style="color: red;">[{{ error }}]</span> {% endfor %} </p> <p>{{ form.submit() }}</p> </form> {% endif %} { % for post in posts %} {% include '_post.html' %} {% endfor %} <p> {{ post.author.username }} says <b>{{ post.body }}</b> </p> {% endblock %}
The expected result is that I am able to log into my local Flask website and see other users posts on the webpage.
-
Astik Anand almost 5 yearsI don't think there is anything different you have posted than already provided answer.
-
Cat almost 4 yearsi upvoted, the answer given by Rohith is absolutely correct, i had the same issue, thx.
-
Pyrocater over 2 yearsI have had a similar issue also with doing the wrong ending:
{# some comment %}