it is my first time asking here as I've tried various methods and solutions but still unable to solve this problem. I always get stuck on "buy handles valid purchase - expected to find "112.00" in page, but it wasn't found". I believe my buy section is already working and the problem is only on the index section as the results are not displayed in the homepage table (not sure why). Here are my index section in app.py and index.html file:
app.py:
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
total_money = 0
stocks = db.execute("SELECT shares, symbol FROM buyer WHERE id = :id", id = session["user_id"])
for stock in stocks:
stock = lookup(request.form.get("symbol"))
shares = int(request.form.get("shares"))
price = float(stock["price"])
total = float(shares*price)
total_money += total
new_money = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
total_money += new_money[0]["cash"]
stocks = db.execute("SELECT * FROM buyer WHERE id = :id", id = session["user_id"])
return render_template("index.html", stocks=stocks, cash = usd(new_money[0]["cash"]), total = usd(total_money))
index.html:
{% extends "layout.html" %}
{% block title %}
Index
{% endblock %}
{% block main %}
<table class="table">
<tr>
<th>Symbol</th>
<th>Shares</th>
<th>Price</th>
<th>Total</th>
</tr>
{% for stock in stocks %}
<tr>
<th> {{ stock["symbol"] }} </th>
<td> {{ stock["shares"] }} </td>
<td> {{ stock["price"] | usd }} </td>
<td> {{ stock["total"] | usd }} </td>
</tr>
{% endfor %}
<tr>
<td>CASH</td>
<td></td>
<td></td>
<td>{{ cash }}</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>{{ total }}</td>
</tr>
</table>
{% endblock %}
for some reason only the 'cash' and 'total' are displayed and the rest doesn't come out. I'm not really sure about jinja and flask syntaxes I just used the lecture examples and references, is there something I'm missing in the way I declare the variables? I would greatly appreciate it if somebody could help check out my code. Thanks in advance!
id
column of thebuyer
table?id
is a key field in the "holdings" table (in this case "buyer"), anduser_id
is the name of theuser_id
column. Sorry for the misdirect.