I'm having issues figuring out why i'm running into this error. I've tried looking online and seeing what answers I could find but I'm stuck. Any help would be greatly appreciated.
Error is: :( buy handles valid purchase expected to find "112.00" in page, but it wasn't found
Code:
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "POST":
symbol = request.form.get("symbol").upper()
item = lookup(symbol)
if not symbol:
return apology("Please enter a symbol")
elif not item:
return apology("Invalid symbol")
try:
shares = int(request.form.get("shares"))
except:
return apology("Shares must be an integer")
if shares <= 0:
return apology("Shares must be a positive integer")
user_id = session["user_id"]
cash = db.execute("SELECT cash FROM users WHERE id = ?", user_id)[0]["cash"]
item_price = item["price"]
total_price = item_price * shares
if cash < total_price:
return apology("not enough cash")
else:
date = datetime.datetime.now()
db.execute("UPDATE users SET cash = ? WHERE id = ?", cash - total_price, user_id)
db.execute("INSERT INTO transactions (user_id, shares, price, symbol, date) VALUES (?, ?, ?, ?, ?)", user_id, shares, item_price, symbol, date)
return redirect("/")
else:
return render_template("buy.html")
Index HTML:
{% extends "layout.html" %}
{% block title %}
Portfolio
{% endblock %}
{% block main %}
<table class="table">
<thead>
<tr>
<th scope="col">Symbols</th>
<th scope="col">Shares</th>
<th scope="col">Price</th>
<th scope="col">TOTAL</th>
</tr>
</thead>
<tbody>
{% for row in database %}
<tr>
<td>{{ row["symbol"] }}</td>
<td>{{ row["shares"] }}</td>
<td>{{ row["price"] | usd() }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<th scope="1">Cash</th>
<th scope="1">{{ cash | usd() }}</th>
</tr>
</tfoot>
</table>
{% endblock %}
Buy HTML:
{% extends "layout.html" %}
{% block title %}
Buy
{% endblock %}
{% block main %}
<h1>Buy</h1>
<form action="/buy" method="post">
<div class="mb-3">
<input autocomplete="off" autofocus class="form-control mx-auto w-auto" name="symbol" placeholder="Symbol" type="text">
</div>
<div class="mb-3">
<input autocomplete="off" autofocus class="form-control mx-auto w-auto" name="shares" placeholder="Shares" type="text">
</div>
<button class="btn btn-primary" type="submit">Buy</button>
</form>
{% endblock %}