I have been trying to implementing my code for Index and index.html. My code for Index seems to work very well, but my index.html isn't. My implementation of index() and index.html are pasted below. For some reasons, the table is printed perfectly, as well as the table headers and the user's current cash balance and total asset, with the exception that all rows inside the for loop simply do not show up. Any advice why? I posted a similar question yesterday night, available here: CS50 Pset7: rows inside the for loop aren't showing up in index.html Thank you very much and I appreciate any help!!!
index():
@app.route("/")
@login_required
def index():
"""User's cover page."""
# Pull past transactions from database
try:
stocks = db.execute("SELECT stock, sum(share) AS total_shares FROM portfolio WHERE user_id = :user_id \
GROUP BY stock ORDER BY stock", user_id=session.get("user_id"))
except RuntimeError:
return apology("welcome to the financial market")
# Caluculate how much user's stocks are worth in total
stock_values = 0
latestprices = []
for stock in stocks:
quote = lookup(stock["stock"])
stock_values = stock_values + quote["price"] * stock["total_shares"]
latestprices.append(quote["price"])
# query database for user's current cash balance
cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session.get("user_id"))
# Caculate user's current total asset
total_asset = stock_values + cash[0]["cash"]
total_asset = str(round(total_asset, 2))
return render_template("index.html", cash=str(round(cash[0]["cash"], 2)), total_worth=total_asset)
index.html:
{% extends "layout.html" %}
{% block title %}
Index
{% endblock %}
{% block main %}
<table border="3">
<tr>
<th>Stocks</th>
<th>Total Shares</th>
<th>Current Prices</th>
</tr>
{% for stock in stocks %}
<tr>
<td>{{stock["stock"]}}</td>
<td>{{stock["total_shares"]}}</td>
</tr>
{% endfor %}
{% for latestprice in latestprices %}
<td>
<tr>{{latestprice}}</tr>
</td>
{% endfor %}
<tr>
<td></td>
<td></td>
<td>Your Current Cash Balance: ${{cash}}</td>
</tr>
<tr>
<td></td>
<td></td>
<td>Your Total Asset: ${{total_worth}}</td>
</tr>
</table>
{% endblock %}