python - CS50 财务:/index 帮助显示正确的信息
问题描述
我看过一些关于 /index 的 CS50 Finance 帮助问题。我正在尝试获取显示用户拥有的股票信息(股票编号、价值、价格等)的路线。现在它显示正确的份额数量,但不显示名称、价值或价格(全为空白)。显示“cash”和“grandTotal”金额,但grandTotal 金额不正确。我想我只是对如何访问我的回报中的特定值感到困惑。
蟒蛇/sqlite:
def index():
"""Show portfolio of stocks"""
# sql queries to select stock info
user_id = session["user_id"]
stocks = db.execute("SELECT stock, symbol, SUM(shares) AS totalShares FROM purchases WHERE userid == :userid GROUP BY symbol", userid=user_id)
currentCash = db.execute("SELECT cash FROM users WHERE id == :userid", userid=user_id)
# Global variables to be updated
tableInfo = []
grandTotal = currentCash[0]["cash"]
#Grabbing info from each owned stock
for stockInfo in stocks:
symbol = stocks[0]["symbol"]
shares = stocks[0]["totalShares"]
name = stocks[0]["stock"]
currentStock = lookup(symbol)
price = currentStock["price"]
value = price * shares
grandTotal += value
tableInfo.append(stockInfo)
# Display a table with portfolio info for current user
return render_template("index.html", tableInfo=tableInfo, grandTotal=usd(grandTotal), currentCash=usd(currentCash[0]["cash"]))
HTML:
{% extends "layout.html" %}
{% block title %}
Your Portfolio
{% endblock %}
{% block main %}
<table class="table">
<thead>
<tr>
<th scope="col">Stock</th>
<th scope="col">Number of shares</th>
<th scope="col">Current price</th>
<th scope="col">Total value</th>
</tr>
</thead>
<tbody>
{% for stock in tableInfo %}
<tr>
<td>{{ stock.name }}</td>
<td>{{ stock.totalShares }}</td>
<td>{{ stock.price }}</td>
<td>{{ stock.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<table class="table">
<thead>
<tr>
<th scope ="col">Cash remaining</th>
<th scope ="col">Grand total</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ currentCash }}</td>
<td> {{ grandTotal }}</td>
</tr>
</tbody>
</table>
{% endblock %}
解决方案
推荐阅读
- python - 模型错误太小,可能是数据类型问题
- android - 我想了解更多有关 Android 相机的信息。谁能给我推荐一个相机应用程序源代码
- android - 开发支付网关?
- angular - 带有查询参数的角延迟加载不起作用
- java - Groovy:一个地图中元素的更改会影响该地图副本中的元素
- sql-server-2012 - SQL Server query to return missing line numbers of range as blank in resultset
- docker - 此处不允许使用 Docker 序列条目
- php - laravel中的文件上传不断返回null
- javascript - 理解 react-redux 中 connect() 函数的语法
- vim - 如何使用底部/向上键提高 MacOS Mojave 的 vim 垂直滚动速度?