[英]CS50 Finance - index not displaying Portfolio correctly
I am having trouble getting the code to properly display the portfolio in index.html.我无法让代码正确显示 index.html 中的投资组合。
My logic with this function is to get a list of all the stocks and cash one user has, and then in a "for" loop, look up the company name and current price for each stock, the total value, and then insert all of that information into a new list of dictionaries (display_portfolio).我对这个 function 的逻辑是获取一个用户拥有的所有股票和现金的列表,然后在“for”循环中,查找每只股票的公司名称和当前价格,总值,然后插入所有将该信息放入一个新的字典列表 (display_portfolio)。 Then the render_template should display "display_portfolio" with this information, as well as the user's total cash and total value of everything.
然后render_template 应该显示“display_portfolio”和这个信息,以及用户的总现金和所有东西的总价值。 However, with my current setup, it is displaying the total cash and grand total, but nothing from the individual stocks.
但是,在我当前的设置中,它显示的是总现金和总计,但没有显示个股。 I am really not sure why that is, and I am unsure if the issue is in my html or in the flask function itself.
我真的不知道为什么会这样,我不确定问题是在我的 html 还是在 flask function 本身。
This is the function:这是 function:
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
# Retrive portfolio
portfolio = db.execute("SELECT symbol, SUM(amount) as amount FROM purchases WHERE id = ? ORDER BY symbol", (session["user_id"]))
user_cash = db.execute("SELECT cash FROM users WHERE id = ?", session["user_id"])
cash = user_cash[0]["cash"]
display_portfolio = []
shares_total = 0
# loop through portfolio symbols to access name and share price for each symbol
for row in portfolio:
requested_quote = lookup(row["symbol"])
symbol = row["symbol"]
amount = row["amount"] #shares
price = float(requested_quote["price"])
name = requested_quote["name"]
share_total = (float(amount) * price)
shares_total = shares_total + share_total
display_portfolio.append({'symbol':symbol, 'name':name, 'shares':amount, 'price':price, 'share_total':share_total})
grand_total = shares_total + cash
return render_template("index.html", display_portfolio = display_portfolio, cash = cash, grand_total = grand_total)
This is index.html:这是索引。html:
{% extends "layout.html" %}
{% block title %}
Portfolio
{% endblock %}
{% block main %}
<div>
<table class="table table-striped">
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>TOTAL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3"></td>
<td>TOTAL</td>
<td>{{ grand_total | usd }}</td>
</tr>
</tfoot>
<tbody>
{% for row in display_portfolio %}
<tr>
<td>{{ display_portfolio.symbol }}</td>
<td>{{ display_portfolio.name }}</td>
<td>{{ display_portfolio.shares }}</td>
<td>{{ display_portfolio.price }}</td>
<td>{{ display_portfolio.total }}</td>
</tr>
{% endfor %}
<td colspan="3"></td>
<td>Cash</td>
<td>{{ cash | usd }}</td>
</tbody>
</table>
</div>
{% endblock %}
I should also note, that when I add "| usd" to "display_portfolio.price" in that it reads:我还应该注意,当我将“| usd”添加到“display_portfolio.price”时,它会显示:
<td>{{ display_portfolio.price | usd }}</td>
I am also getting a completely separate error, and not sure why it would work with cash and not this.我也得到了一个完全独立的错误,不知道为什么它可以用现金而不是这个。
I can confirm that there exists purchases in the sql database the "portfolio" variable is retrieving.我可以确认 sql 数据库中存在“投资组合”变量正在检索的购买。
This is what it looks like:这是它的样子:
Any help will be appreciated, thanks!任何帮助将不胜感激,谢谢!
FromMDN doc on tfoot :来自tfoot 上的 MDN 文档:
Permitted parents: A
<table>
element.允许的父母:一个
<table>
元素。 The<tfoot>
must appear after any<caption>
,<colgroup>
,<thead>
,<tbody>
, or<tr>
element.<tfoot>
必须出现在任何<caption>
、<colgroup>
、<thead>
、<tbody>
或<tr>
元素之后。 Note that this is the requirement as of HTML5.请注意,这是 HTML5 的要求。
Suspect the Cash line is displayed because it is missing <tr>
tags.怀疑 Cash line 已显示,因为它缺少
<tr>
标记。
Nu HTML Validator from W3C is your friend:)来自 W3C 的Nu HTML 验证器是你的朋友:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.