0

I can't figure out what is wrong in my code

My python code:

def buy(): """Buy shares of stock"""

if request.method == "GET":   
    render_template("buy.html")
    
elif request.method == "POST":
    symbol = request.form.get(symbol)
    quote = lookup(symbol)
    
    # Check if the symbol is valid
    if not quote:
        return apology("Invalid symbol", 400)
    
    # Check if the number of shares is an integer greater than 0 
    shares = request.form.get(shares)
    if shares <= 0:
        return apology("Invalid number of shares", 400)
    
    # Get the user from ther users database
    cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session.get("user_id"))
    username = db.execute("SELECT username FROM users WHERE id = :user_id", user_id = session.get("user_id"))
    # Check if the user has enough money
    price = quote["price"]
    total = price*shares
    if total > cash:
        return apology("You don't have enough money", 400)
   
    # datetime object containing current date and time
    now = datetime.now()
    
    # dd/mm/YY H:M:S
    date = now.strftime("%d/%m/%Y %H:%M:%S")

    db.execute("INSERT INTO history (symbol, price, qtd, date, username) VALUES (:symbol, :price, :qtd, :date, :username)", symbol=symbol, price=price, qtd=shares, date=date, username=username)   
    
    # Update the cash value
    update_cash = (cash - total)
    db.execute("UPDATE users SET cash=:update_cash WHERE id=:user_id", update_cash=update_cash, user_id=session.get("user_id"))
    return redirect("/")

My HTML code:

<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>

The Type Error: The view function did not return a valid response. The function either returned None or ended without a return statement.

1
  • missing "return" in the GET branch Jul 10 '20 at 23:32
0

you need to return render_template

You must log in to answer this question.

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