Question about jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endfor'. Jinja was looking for the following tags: 'endblock'

∥☆過路亽.° 提交于 2021-01-07 07:01:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!