我當前的代碼返回大量沒有數據的行。不確定我對 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] 將為您提供列表中的第一個字典?,F在,當您使用語法 tables[0][0] 時,您正在嘗試使用 0(零)作為鍵來訪問字典。如果沒有密鑰 0(零),您將收到密鑰錯誤。
添加回答
舉報
0/150
提交
取消