I'm currently on CS50 PSET 9 Finance and stuck on the index.html portion of the assignment.
This is my code from assignment.py:
@app.route("/")
@login_required
def index():
stocks = db.execute("SELECT * FROM stockbuy WHERE id = ?", session.get("user_id"))
cash = db.execute("SELECT * FROM users WHERE id = ?", session.get("user_id"))
cash_remain = float(cash[0]["cash"])
for stock in stocks:
tmp_cash = (float(stock["price"]) * float(stock["quantity"]))
cash_remain = cash_remain - tmp_cash
cash_remain = round(cash_remain, 2)
return render_template("index.html", currentcash=cash_remain, stocks=stocks)
My HTML:
{% extends "layout.html" %}
{% block title %}
Buy
{% endblock %}
{% block main %}
<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="4"></td>
<td>{{ totalvalue }}</td>
</tr>
</tfoot>
<tbody>
{% for stock in stocks %}
<tr>
<td>{{ stock["symbol"] }}</td>
<td>{{ stock["name"] }}</td>
<td>{{ stock["quantity"] }}</td>
<td>{{ stock["price"] }}</td>
<td>{{ stock["price"] * stock["quantity"] }}</td>
</tr>
{% endfor %}
<tr>
<td colspan="4">CASH</td>
<td>{{ currentcash }}</td>
</tr>
</tbody>
</table>
{% endblock %}
I have yet to implement {{ totalvalue }} as well.
Below is my Finance page:
Below is my database for stocks:
The issue I'm facing is that although i have multiple shares bought, i am unable to get the {%for%} loop in index.html to run over all the shares. I have tried to troubleshoot by passing different variables through the render_template, but I'm unsure of how to manipulate the data from db.execute through render_template and into html.
Any advice on which areas i should focus on would be much appreciated.