0

This is my first time posting on reddit asking for help : I've been so far going through cs50 on ressources online when I got stuck, but here I can't figure it out even tho with the numerous online walkthroughs that exist.

So, my dropdown menu isn't showing up : it seems it can't get the list of symbols I sent on the Jinja loop. Here is my sell function code : ( I bolded ) what I think is the important part I'm stuck with both on the app.py and index.html files @.route("/sell", methods=["GET", "POST"]) @login_required def sell(): if request.method == "POST":

    # Ensure user typed a symbol
    symbol = request.form.get("symbol").upper()
    if not symbol:
        return apology("must provide a symbol", 403)

     # Ensure symbol exist
    item = lookup(symbol)
    if not item:
        return apology("Invalid symbol", 403)

    # Return apology if the user does not own any shares of that stock after submission
    user_id = session["user_id"]
    rows = db.execute("SELECT symbol FROM trades WHERE user_id = ?", user_id)
    if symbol not in rows:
        return apology("You don't own any shares of this stock, you malicious")

    # Get the number of shares, ensure it's a positive integer
    try:
        shares = int(request.form.get("shares"))
    except:
        return apology("Must provide an entire number of shares")
    if shares <= 0:
        return apology("Must provide a positive number of shares to sell")

    # Ensure user has enough shares
    nbshares_user = db.execute("SELECT SUM(shares) as totalShares FROM trades WHERE symbol = ? AND user_id = ? HAVING totalShares > 0", symbol, user_id)
    if shares > nbshares_user:
        return apology("You don't have that many shares")

    # Obtain price of sell
    item = lookup(symbol)
    item_price = item["price"]
    total_price = item["price"]*shares

    # L'additionner à son cash
    rows = db.execute("SELECT cash FROM users WHERE id = ?", user_id)
    cash=rows[0]["cash"]
    new_cash = cash + total_price


    # Update database users cash
    db.execute("UPDATE users SET cash= ? WHERE id = ?", new_cash, user_id)

    # Update user trades
    db.execute("INSERT INTO trades (user_id, symbol, price, shares, type) VALUES(?, ?, ?, ?, ?)", user_id, symbol, item_price, -shares, 'sell')

    # Get a list of symbols for the select option
    symbols = db.execute("SELECT DISTINCT symbol FROM trades WHERE user_id = ?", user_id)
    return render_template("sell.html", symbols=symbols)
    flash("sold!")
    redirect("/")
else:
    return render_template("sell.html")

And here is the sell.html part involved:

<form action="/sell" method="post">

    <div class="form-group">

        <select name="symbol" >
             <option disabled selected value="">Symbol</option>
            {% for symbols in symbols %}
            <option value = "{{ symbols.symbol }}">{{ symbols.symbol }}</option>
            {%endfor%}

        </select>
    </div>
    <div class="form-group"></div>
        <input autocomplete="off" name="shares" type="text" class="form-control"placeholder="Enter a number of shares">
    </div>
    <button class="btn btn-primary" type="submit">Sell</button>
</form>

{% endblock %} It would basically just display the scroll down menu, but without anything in it. I've been looking on almost every solutions that I found, and there is quite a few posts on here only about the dropdown select menu, but nothing that helped me solving the problem. If you have any idea, I feel stuck and kinda frustrated. Thanks for your support Etienne

1 Answer 1

0

it seems it can't get the list of symbols I sent on the Jinja loop

The dropdown list of valid symbols is needed for the GET method. Program does not "send" the list of symbols in the GET branch.

else:
    return render_template("sell.html")

You must log in to answer this question.

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