I have to show current value of my stock which is stored in my data base, I have to multiply Number of shares to current price here is my code
@app.route("/")
@login_required
def index():
#Remember id
ide = session["user_id"]
#take info about sahres user already bought
symb = db.execute ("select * from portfolio where id = :ii GROUP BY symbol ORDER BY symbol" , ii = ide)
#if user has not bought any sahres
if not symb:
#Return user a message in html and his cash holdings
row = db.execute ("select * from users where id = :ii" , ii = ide)
return render_template ("noshares.html",cash = row[0]["cash"])
else:
#list to save current price of stock
curprice = []
#Iterrate over each element in symb
for share in symb:
#Get symbol of each element
symbol = share ["symbol"]
#Lookup current price of that specific symbol/sahre
quote = lookup (symbol)
#save each current price
curprice.append (quote["price"])
#render values
return render_template ("index.html", symbol = symb,currentprices = curprice)
and here is html code
{% extends "layout.html" %}
{% block title %}
Portfolio
{% endblock %}
{% block main %}
<table>
<tr>
<th>Stocks</th>
<th>Shares</th>
<th>Current Price</th>
<th>Total</th>
</tr>
{% for stock in symbol %}
<tr>
<td>{{ stock["symbol"] }}</td>
<td>{{ stock["shares"] }}</td>
<td>{{ currentprices[stock] }}</td>
<td>{{stock["shares"] * currentprices[stock]}} </td>
</tr>
{% endfor %}
{% endblock %}
This code shows error jinja2.exceptions.UndefinedError: list object has no element {'symbol': 'nflx', 'price': 281.78, 'shares': 2, 'id': 3}
In the error you can see the info from SQL data base and error is occuring on the last line which is
<td>{{stock["shares"] * currentprices[stock]}} </td>