有关jinja2.exceptions.TemplateSyntaxError的问题:遇到未知标签'endfor'.Jinja在寻找以下标签:'endblock' [英] Question about jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endfor'. Jinja was looking for the following tags: 'endblock'

查看:544
本文介绍了有关jinja2.exceptions.TemplateSyntaxError的问题:遇到未知标签'endfor'.Jinja在寻找以下标签:'endblock'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Flask构建网站,并且遇到了 jinja2.exceptions.TemplateSyntaxError:遇到未知标签'endfor'.Jinja正在寻找以下标签:'endblock'.需要关闭的最里面的块是块" 错误.当我尝试使用localhost:5000登录到本地网页时,出现上述错误.

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.

我已经阅读了追溯错误,并且似乎发生在index.html文件的第21行.

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.

推荐答案

您在第二个for循环中输入了错字

You have a Typo, in the second for-loop

{ % for post in posts %}
       {% include '_post.html' %}

      {% endfor %}

应如下所示:

    {% for post in posts %}
   {% include '_post.html' %}
{% endfor %}

原因:由于 {后的之前有空格,因此Jinja无法将其识别为endfor标记.从而给您错误.

The Reason: Since there is space after { before % Jinja will not recognize it as an endfor tag. thus giving you the error.

这篇关于有关jinja2.exceptions.TemplateSyntaxError的问题:遇到未知标签'endfor'.Jinja在寻找以下标签:'endblock'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆