我被卡住了,试图让我的烧瓶输入功能正常工作。我正在努力完成CS50 2020的财务问题。有关该问题的文档可以在here中找到。下面的代码块是当我尝试使用索引函数运行时在控制台中得到的错误消息。我使用的api密钥是有效的,当为get请求插入整个url时,我得到了预期的信息。代码可能有点乱,我还没有清理和优化它,因为我不能让它工作。
DEBUG: SELECT quantity FROM oStocks WHERE userID = 5 AND stock = 'AAPL'
DEBUG: Starting new HTTPS connection (1): cloud.iexapis.com:443
DEBUG: https://cloud.iexapis.com:443 "GET /stable/stock/GOOGL-AF/quote?token=<MY_API_KEY> HTTP/1.1" 200 None我已经尝试了我能想到的所有方法来尝试修复这个错误。更改和更改了几乎所有关于查找函数的内容。我似乎找不到哪里出了问题。有人能给我指个方向吗?谢谢
烧瓶索引:
@app.route("/")
@login_required
def index():
userid = session["user_id"]
owned = db.execute("SELECT stock FROM oStocks WHERE userID = :userid", userid=userid)
OwnedStocks = []
for row in owned:
OwnedStocks.append(row["stock"])
stockInfo = {}
for item in OwnedStocks:
itemInfo = lookup(item)
tmpQuantity = db.execute("SELECT quantity FROM oStocks WHERE userID = :userid AND stock = :stock", userid=userid, stock=item)
quantity = tmpQuantity[0]["quantity"]
sharePrice = itemInfo["price"]
name = itemInfo["name"]
value = quantity * sharePrice
stockInfo[item] = {}
stockInfo[item]['symbol'] = item
stockInfo[item]['name'] = name
stockInfo[item]['shares'] = quantity
stockInfo[item]['price'] = sharePrice
stockInfo[item]['value'] = value
return render_template("portfolio.html", stocks=stockInfo)查找函数:
def lookup(symbol):
"""Look up quote for symbol."""
# Contact API
try:
api_key = os.environ.get("API_KEY")
response = requests.get(f"https://cloud.iexapis.com/stable/stock/{urllib.parse.quote_plus(symbol)}/quote?token={api_key}")
response.raise_for_status()
except requests.RequestException:
return None
# Parse response
try:
quote = response.json()
return {
"name": quote["companyName"],
"price": float(quote["latestPrice"]),
"symbol": quote["symbol"]
}
except (KeyError, TypeError, ValueError):
return NonePortfolio html:
{% extends "layout.html" %}
{% block title %}
Portfolio
{% endblock %}
{% block main %}
<div>
<table class="table table-hover">
<thead>
<tr class="font-weight-bold">
<th scope="col">Symbol</th>
<th scope="col">Name</th>
<th scope="col">Shares</th>
<th scope="col">Price</th>
<th scope="col">Total</th>
</tr>
</thead>
<tbody>
{% for stock in stocks %}
<tr>
<th scope="row">{{ stock['symbol'] }}</th>
<td>{{ stock['name'] }}</td>
<td>{{ stock['shares'] }}</td>
<td>{{ stock['price'] }}</td>
<td>{{ stock['total'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}发布于 2020-12-21 19:40:37
我已经找到了解决方案,现在找到它了,我觉得自己很愚蠢,因为这么长时间以来我一直在寻找所有错误的地方。我的错误是迭代和调用传递给flask模板的字典中的值。经过多次搜索,我终于在使用camposha时意识到并解决了我的问题。
我最初的方法是:
{% for stock in stocks %}
<tr>
<th scope="row">{{ stock['symbol'] }}</th>
<td>{{ stock['name'] }}</td>
<td>{{ stock['shares'] }}</td>
<td>{{ stock['price'] }}</td>
<td>{{ stock['total'] }}</td>
</tr>
{% endfor %}然而,实现我的目标的正确方法是:
{% for stock, value in stocks.items() %}
<tr>
<th scope="row">{{ value['symbol']['stock'] }}</th>
<td>{{ value['name'] }}</td>
<td>{{ value['shares'] }}</td>
<td>{{ value['price'] }}</td>
<td>{{ value['value'] }}</td>
</tr>
{% endfor %}我也对我的索引做了一些调整:
def index():
userid = session["user_id"]
owned = db.execute("SELECT stock, quantity FROM oStocks WHERE userID = :userid", userid=userid)
stockInfo = {}
for item in owned:
print(item)
itemInfo = lookup(item['stock'])
quantity = item['quantity']
name = itemInfo['name']
sharePrice = float(itemInfo['price'])
value = quantity * sharePrice
stockInfo[item['stock']] = {}
stockInfo[item['stock']]['symbol'] = item
stockInfo[item['stock']]['name'] = name
stockInfo[item['stock']]['shares'] = quantity
stockInfo[item['stock']]['price'] = sharePrice
stockInfo[item['stock']]['value'] = value
return render_template("portfolio.html", stocks=stockInfo)https://stackoverflow.com/questions/65389601
复制相似问题