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"])
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
            shares = int(request.form.get("shares"))
            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)


        return redirect("/")
        return render_template("buy.html")

The buy.html code is:

{% extends "layout.html" %}

{% block title %}
{% 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 class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="shares" placeholder="Shares" type="number">

        <button class="btn btn-primary" type="submit">Buy</button>
{% endblock %}

Thank you!

  • Did you check the traceback?
    – Vsjain
    Oct 15, 2020 at 7:56
  • I'm unfamiliar with the traceback. Can you please clarify? Oct 15, 2020 at 13:15
  • When you receive the error, go to your ide terminal. There you will obtain a traceback for the error that has occured. If you have difficulty comprehending it, please add that to the question.
    – Vsjain
    Oct 15, 2020 at 13:37
  • I was able to figure it out. I had an error on the sql code that I corrected. Thank you! Oct 15, 2020 at 23:11


You must log in to answer this question.

Browse other questions tagged .