问题
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.
回答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.
来源:https://stackoverflow.com/questions/57081082/question-about-jinja2-exceptions-templatesyntaxerror-encountered-unknown-tag-e