首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jinja中的jinja2.exceptions.TemplateSyntaxError (即使我在代码中写了{% endfor%},for循环也没有结束)

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

Stack Overflow用户
提问于 2019-03-04 18:53:23
回答 1查看 1K关注 0票数 0

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

代码语言:javascript
运行
复制
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,jinja2版本是2.10

请查看这些代码并帮助我

烧瓶代码:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
{% 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 %}

请帮帮我,感谢您的帮助。

谢谢

EN

Stack Overflow用户

发布于 2019-03-04 20:18:02

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

代码语言:javascript
运行
复制
{% 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 %}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54981755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档