我当前的代码返回大量没有数据的行。不确定我对 jinja2 模板中的 for 循环做错了什么。我尝试{{ tables[0][0] }}在 for 循环内使用但收到错误。jinja2.exceptions.TemplateSyntaxError: 预期的标记 ':',得到 '}'使用 jquery 之类的东西从 JSON 创建 HTML 表会更好吗?app.py(缩短代码)@app.route('/', methods=['GET', 'POST'])def index(): def stockOwnership(ticker): ... # prints dataframe to html table_13D = df_13D.to_json(orient='records') table_13F = df_13F.to_json(orient='records') print(table_13D) #variables holding functions to be passed into tables list ownership = stockOwnership(stock) return render_template('index.html', tables=[ownership])索引.html{% extends 'base.html' %}{% block title %} <title>Stock Info</title>{% endblock %}{% block body %} <h1>Ownership</h1> <center><h4 class="title-name">13D/G Filings</h4></center> <table class="table table-striped", id='13D'> <thead> <tr class="bg-info"> <th>File Date</th> <th>Form</th> <th>Investors</th> <th>Shares</th> </tr> </thead> <tbody id="mydata"> {%for data in tables[0][0]%} <tr> <td>{{data.File_Date}}</td> <td>{{data.Form}}</td> <td>{{data.Investors}}</td> <td>{{data.Shares}}</td> </tr> {%endfor%} </tbody> </table> json 通过 {{ 表 [0][0] }}[ { "File_Date":"2020-04-29", "Form":"13D\/A", "Investors":"ARMISTICE CAPITAL, LLC", "Shares":"407,373" }, { "File_Date":"2020-03-23", "Form":"13G", "Investors":"INTRACOASTAL CAPITAL, LLC", "Shares":"3,517,022" }, { "File_Date":"2020-03-12", "Form":"13G", "Investors":"Sabby Management, LLC", "Shares":"6,000,000" },]
2 回答
波斯汪
TA贡献1811条经验 获得超4个赞
不确定这{{ tables[0][0] }}是否是访问 of 类型dict的第一个元素list的有效方法dicts。据我了解,您目前有一个listof dicts,因此如果您想将单个数据字典更新为一行,那么您可以使用这样的语法。
{% for dict_item in parent_list %}
{% for key, value in dict_item.items() %}
<h1>Key: {{key}}</h1>
<h2>Value: {{value}}</h2>
{% endfor %}
{% endfor %}
因此,您可以使用<td>Value: {{value}}</td>来呈现<td>一行中的值而不是<td>{{data.File_Date}}</td>. 这应该可以有效地解决您的问题。
潇潇雨雨
TA贡献1833条经验 获得超4个赞
我认为以下行是错误的{%for data in tables[0][0]%}
在此上下文中,您从表中获取数据,表是包含字典的列表。tables[0] 将为您提供列表中的第一个字典。现在,当您使用语法 tables[0][0] 时,您正在尝试使用 0(零)作为键来访问字典。如果没有密钥 0(零),您将收到密钥错误。
添加回答
举报
0/150
提交
取消
