jinja中的jinja2.exceptions.TemplateSyntaxError(即使我在代码中写了{%endfor%},for循环也未结束)

时间:2019-03-04 10:53:23

标签: python flask jinja2

即使我关闭了for循环,该错误也总是会出现:

jinja2.exceptions.TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.

我的python版本是python3.6,flask版本是1.0.6,jinja 2版本是2.10

请查看这些代码并为我提供帮助

烧瓶代码:

from flask import *
import pandas
app = Flask(__name__, template_folder='templates')
@app.route('/')
def index():
    test = pandas.read_csv('test.csv')
    test0 = test['PassengerId']
    test1 = test['Name']
    print(test0, test1)
    return render_template('index.html', tables={'test':test0,'test1':test1})

if __name__ == '__main__':
    app.debug = True
    app.run()

templates / index.html:

{% block content %}
<head>
    <title>pandas app</title>
</head>
{% block body %}
    {% for test0,test1 in tables %}
        <TABLE>
        <thead>
        <tr>
         <th>PassengerId</th>
         <th>Name</th> 
        </tr>
        </thead>
        <tbody>

          <tr>
          <td>{{ test0 }}</td>
          <td>{{ test1 }}</td>
          </tr>
          </tbody>
          </TABLE>

    {% endfor %}
{% endblock %}
{% endblock %}

请帮助我,感谢您的帮助

谢谢

1 个答案:

答案 0 :(得分:1)

您可以一次阻止多个块,因此将它们分开:

{% block content %}
<head>
    <title>pandas app</title>
</head>

{% endblock %}

{% block body %}
    {% for test0,test1 in tables %}
        <TABLE>
        <thead>
        <tr>
         <th>PassengerId</th>
         <th>Name</th> 
        </tr>
        </thead>
        <tbody>

          <tr>
          <td>{{ test0 }}</td>
          <td>{{ test1 }}</td>
          </tr>
          </tbody>
          </TABLE>

    {% endfor %}

{% endblock %}