【发布时间】:2019-12-24 02:45:10
【问题描述】:
我完全被cs50金融中的索引功能所困!这个函数应该在一个 html 页面中返回一个表格,其中包含在线交易的详细信息(详细信息保存在数据库中)。但它不起作用:即使我的数据库中有事务,我的函数也找不到它,表是空的。 这是我的代码:
def index():
"""Show portfolio of stocks"""
rows = db.execute("SELECT symbol, price, shares FROM transactions WHERE id = :user_id", user_id=session["user_id"])
transactions_info = []
total_worth = 0
for transaction_info in rows:
symbol = rows [0]["symbol"]
shares = rows [0]["shares"]
price = lookup(symbol)
worth = shares * price ["price"]
total_worth += worth
transactions_info.append(transaction_info)
return render_template("index.html", rows=rows, transactions_info=transactions_info)
这是我的 HTML 页面:
{% extends "layout.html" %}
{% block title %}
Index
{% endblock %}
{% block main %}
<table class="table table-striped" style="width:100%">
<tr>
<th>Symbol</th>
<th>Company</th>
<th>Shares</th>
<th>Price</th>
<th>TOTAL</th>
</tr>
{% for transaction in transactions %}
<tr>
<td>{{ transaction_info.symbol }}</td>
<td>{{ transaction_info.name }}</td>
<td>{{ transaction_info.shares }}</td>
<td>{{ transaction_info.price }}</td>
<td>{{ transaction_info.worth }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
感谢您的帮助!
【问题讨论】:
-
这个代码是minimal reproducible example吗?你做了什么来尝试调试这个?
标签: python html database jinja2 cs50