The table is not able to access different variables to display in the table. Here is the index function
def index():
"""Show portfolio of stocks"""
"""Selecting shares that have been purchased already"""
stocks = db.execute("SELECT symbol, shares FROM portfolio WHERE userid = :userid", userid = session["user_id"])
if stocks:
for stock in stocks:
symbol = stock["symbol"]
quantity = stock["shares"]
quote = lookup(symbol)
price = quote["price"]
name = stock["name"]
value = quantity * price
"""to retrieve the current amount of cash the user owns"""
cashs = db.execute("SELECT * FROM users WHERE id=:ida", ida=session["user_id"])
if not cashs:
return apology("cant retrieve cash", 400)
casha = float(cashs[0]["cash"])
if not casha:
return apology("something with retrieving cash")
return render_template("index.html", stocks = stocks, cash = casha)
here is the index.html function
{% extends "layout.html" %}
{% block title %}
Index
{% endblock %}
{% block main %}
<table>
<tr>
<th> Symbol</th>
<th> Name</th>
<th> Shares</th>
<th> Price</th>
<th> Total</th>
</tr>
{% for stock in stocks %}
<tr>
<td>{{stock.symbol}}</td>
<td>{{stock.name}}</td>
<td>{{stock.quantity}}</td>
<td>{{stock.price}}</td>
<td>{{stock.value}}</td>
</tr>
{% endfor %}
<tr>
<td> cash </td>
<td></td>
<td></td>
<td></td>
<td>{{ cash }}</td>
</tr>
</table>
{% endblock %}
sdited code :-
rows = db.execute("SELECT symbol, shares FROM portfolio WHERE userid = :userid", userid = session["user_id"])
if rows:
for stock in rows:
stock["symbol"] = rows["symbol"]
stock["quantity"] = rows["shares"]
quote = lookup(symbol)
stock["price"] = quote["price"]
stock["name"] = quote["name"]
stock["value"] = quantity * price
"""to retrieve the current amount of cash the user owns"""
cashs = db.execute("SELECT * FROM users WHERE id=:ida", ida=session["user_id"])
if not cashs:
return apology("cant retrieve cash", 400)
casha = float(cashs[0]["cash"])
if not casha:
return apology("something with retrieving cash")
return render_template("index.html", stocks = rows, cash = casha)