I am getting the error message "500 Internal Server Error" when trying to buy shares. Can you please tell me what I am missing? The application.py code is:
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "POST":
quote = lookup(request.form.get("symbol"))
#Check if valid symbol
if quote == None:
return apology("Must provide a valid symbol", 400)
#Check if shares are an integer
try:
shares = int(request.form.get("shares"))
except:
return apology("share number must be a positive integer", 400)
#Check if shares entered >0
if shares <= 0:
return apology("shares must be positive", 400)
#Query database for username
rows = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])
#Check the cash balance
cash_remaining = rows[0]["cash"]
price_per_share = quote["price"]
#Calculate cash required to buy shares
total_price = price_per_share * shares
#Check if user has enough money
if total_price > cash_remaining:
return apology("You don't have enough cash")
#Insert new cash into users database
db.execute("UPDATE users SET cash = cash - :price WHERE id = :user_id", price = total_price, user_id=session["user_id"])
#Insert transaction into transactions database
db.execute("INSERT INTO transactions (user_id, symbol, shares, price_per_share, total_price,) VALUES(:user_id, :symbol, :shares, :price_per_share, :total_price)",
user_id = session["user_id"],
symbol = request.form.get("symbol"),
shares = shares,
price_per_share = price_per_share,
total_price = total_price)
flash("Bought!")
return redirect("/")
else:
return render_template("buy.html")
The buy.html code is:
{% extends "layout.html" %}
{% block title %}
Buy
{% endblock %}
{% block main %}
<form action="/buy" method="post">
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="symbol" placeholder="Symbol" type="text">
</div>
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="shares" placeholder="Shares" type="number">
</div>
<button class="btn btn-primary" type="submit">Buy</button>
</form>
{% endblock %}
Thank you!