after I run check 50, everything shows green except these: enter image description here

I am trying to fix the error for buy. After I run flask and tried to make purchase, it shows 404 however my portfolio table get updated. So I am thinking the error is in linking buy code to my index code so my buy function will show in index.html. but I dont know how. Below are my code for index.html. Any suggestions would be appreciated.

{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
    <table class="table table-striped" style="width:100%">
                    <td colspan="4"></td>
                    <td> <b> ${{'%0.2f'|format(total_balance)}} </b> </td>

            {% for row in rows_pt %}
            {% endfor %}
                <td colspan="4">CASH</td>
{% endblock %}

Below are my code for index and buy

def index():
    """Show portfolio of stocks"""
    rows_pt = db.execute("SELECT * FROM portfolio WHERE id = :id",id = session["user_id"])

if rows_pt == []:
    cash_b = db.execute("SELECT cash FROM users WHERE id = :id",id = session["user_id"])
    cash = cash_b[0]["cash"]
    return render_template("index.html", current_balance=cash, total_balance = cash)
    new_rows_pt = []
    for row in rows_pt:
        new_rows_dict = {}
        quote = lookup(row["symbol"])
        new_rows_dict["symbol"] = quote["symbol"]
        new_rows_dict["name"] = quote["name"]
        new_rows_dict["shares"] = row["shares"]
        new_rows_dict["price"] = quote["price"]
        new_rows_dict["total"] = row["shares"] * quote["price"]


    cash_b = db.execute("SELECT cash FROM users WHERE id = :id",id = session["user_id"])
    cash = cash_b[0]["cash"]
    total_balance = cash
    for row in new_rows_pt:
        total_balance += row["total"]
    return render_template("index.html", rows_pt = new_rows_pt, current_balance = cash, total_balance = cash)

@app.route("/buy", methods=["GET", "POST"])
def buy():
"""Buy shares of stock"""
    if request.method == "POST":
        if not request.form.get("symbol"):
            return apology("must provide symbol")

        elif not request.form.get("shares") or not request.form.get("shares").isdigit() or int(request.form.get("shares")) <= 0:
            return apology("must provide positive number of shares you want to purchase")

        quote = lookup(request.form.get("symbol").upper())
        if not quote:
            return apology("invalid stock name")

    cash = db.execute("SELECT cash FROM users WHERE id= :id", id=session["user_id"])
    if not cash or float(cash[0]["cash"]) < quote["price"] * int(request.form.get("shares")):
        return apology("insufficient cash")

    db.execute("UPDATE users SET cash = cash - :price WHERE id = :id", price = quote["price"] * int(request.form.get("shares")), id = session["user_id"])

    updated_cash = db.execute("SELECT cash FROM users WHERE id= :id", id=session["user_id"])
    total = updated_cash[0]["cash"] + quote["price"] * int(request.form.get("shares"))

    rows = db.execute("SELECT * FROM portfolio WHERE id = :id AND symbol = :symbol",symbol = request.form.get("symbol").upper(), id = session["user_id"])
    if len(rows) == 0:
        db.execute("INSERT INTO portfolio (id, symbol, name, shares, price, total) VALUES (:id, :symbol, :name, :shares, :price, :total)", id= session["user_id"], symbol = request.form.get("symbol").upper(), name = quote["name"], shares= int(request.form.get("shares")), price = usd(quote["price"]), total = total)
        db.execute("UPDATE portfolio SET shares = shares + :shares WHERE symbol = :symbol", shares= int(request.form.get("shares")), symbol = request.form.get("symbol").upper())

    db.execute("INSERT INTO history (id, symbol, shares, price) VALUES (:id, :symbol, :shares, :price)", id = session["user_id"], symbol = request.form.get("symbol").upper(), shares = int(request.form.get("shares")), price =quote["price"])

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

Per your code, your route for the index page is

def index():

so return redirect("/index") in buy is going to return a 404.

Try return redirect("/") instead.

  • It worked! Didn't think it was that easy. Thank you so much – Issa Sep 7 '18 at 3:47

You must log in to answer this question.

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