无法使索引功能在 cs50 finance 中工作
can't make index function work in cs50 finance
我完全卡在让索引函数在cs50 finance中工作!此函数应 return 在 html 页面中 table 包含在线交易的详细信息(详细信息保存在数据库中)。但它不起作用:即使我的数据库中有事务,我的函数也找不到它,table 是空的。
这是我的代码:
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 %}
感谢您的帮助!
在 index()
中,您正在向此处发送一个名为 transactions_info
的列表
return render_template("index.html", rows=rows, transactions_info=transactions_info)
在 html 中,您在此处 {% for transaction in transactions %}
.
循环访问名为 transactions
的列表
我完全卡在让索引函数在cs50 finance中工作!此函数应 return 在 html 页面中 table 包含在线交易的详细信息(详细信息保存在数据库中)。但它不起作用:即使我的数据库中有事务,我的函数也找不到它,table 是空的。 这是我的代码:
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 %}
感谢您的帮助!
在 index()
中,您正在向此处发送一个名为 transactions_info
的列表
return render_template("index.html", rows=rows, transactions_info=transactions_info)
在 html 中,您在此处 {% for transaction in transactions %}
.
transactions
的列表