0

I'm trying to get the Buy implementation to work but I keep on getting the error message "Missing Shares" when I log into the cs50 finance site and enter in the symbol and number of shares. Any guidance would be appreciated. Thank you.

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    """Buy shares of stock"""
    if request.method == "POST":

        if not request.form.get("symbol"):
            return apology("Must provide a valid symbol")

        elif not request.form.get("shares"):
            return apology("Missing shares")

        elif not request.form.get("shares").isdigit():
            return apology("Invalid number of shares")

        cash = db.execute("SELECT cash FROM users WHERE id=:id", id = session("user_id"))
        cash = rows[0]['cash']

        updated_cash = balance - shares * price

        if updated_cash <0:
            return apology("You don't have enough cash")
        db.execute("UPDATE users SET cash=:updated_cash WHERE id=:id",
                    updated_cash=updated_cash,
                    id = session["user_id"])
        return redirect("/")
    else:
        return render_template("buy.html")

The code for buy 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="symbol" placeholder="Shares" type="text">
        </div>

        <button class="btn btn-primary" type="submit">Buy</button>
    </form>
{% endblock %}
2
  • Can you add your code for buy.html
    – Vsjain
    Oct 13 '20 at 8:19
  • I included the code for buy.html above. Oct 13 '20 at 11:46
1

The name for shares is "symbol". It is in this line:

<input autocomplete="off" autofocus class="form-control" name="symbol" placeholder="Shares" type="text">

It should be named shares. Also your code is incomplete as it doesn't save/update the stocks and history.

If this helps, the please check the tickmark.

1
  • Thank you for the tip. I'm still working through this. Oct 13 '20 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .