:( buy handles valid purchase application raised an exception (see log for details) Log sending POST request to /login sending POST request to /buy exception raised in application: TypeError: 'NoneType' object is not subscriptable
Above is the output from check50 I've been scratching my head about for a few days now.
1) I've checked that lookup() is returning values. It's always defaulting to the backup as Yahoo seems to be down, so I'm never geting a 'name' of a stock, always the symbol for both the symbol part and the name part.
2) The code runs fine when I try it myself as a website. It returns a newly populated portfolio of stocks with the current price of the shares showing and the total value of each share showing on the right, plus the user's current balance and grand total, just like the staff solution.
3) I don't fully understand what check50 is looking for. It's finding a 'NoneType' so it's presumably parsing something that hasn't populated correctly. Any ideas?
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Get symbol, check validity
symbol = lookup(request.form.get("symbol"))
#print(symbol)
if lookup(request.form.get("symbol")) == None:
return apology("invalid symbol", 400)
try:
shares = float(request.form.get("shares"))
except ValueError:
return apology("Not a number", 400)
# Ensure a number is entered and it is positve
if request.form.get("shares") == None or float(request.form.get("shares")) < 0:
return apology("invalid number", 400)
# Check that the number entered is whole
if is_whole(request.form.get("shares")) == False:
return apology("must be a whole number", 400)
# Check user can afford it.
shares = float(request.form.get("shares"))
acc_balance = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
if acc_balance[0]["cash"] - (symbol["price"] * shares) < 0:
return apology("cannot afford", 400)
# Insert who bought, which symbol, how many shares, time, at what price into table
else:
db.execute("INSERT INTO transactions (symbol, user_id, at_price, time_date,shares) \
VALUES (:symbol, :user_id, :at_price, :time_date,:shares)", \
symbol=symbol["symbol"], user_id=session["user_id"], at_price=float(symbol["price"]), time_date=datetime.datetime.now().strftime("%d-%m-%y %H:%M"),shares=request.form.get("shares"))
# Update user's cash
db.execute("UPDATE users SET cash = cash - :price WHERE id = :user_id", \
price=float(symbol["price"]) * float(request.form.get("shares")), user_id=session["user_id"])
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("buy.html")
*
This is index.html
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
# Think dicts in a list...
txns = db.execute("SELECT symbol, shares FROM transactions WHERE user_id = :id", id=session["user_id"])
# Add the symbols of the stocks on hand to a list
basket = get_basket(txns)
portfolio = get_portfolio(basket)
#print(portfolio)
gtotal = 0.0
for i in range(len(portfolio)):
for j in range(len(portfolio[i])):
# Insert the current share price into the portfolio
symbol = lookup(portfolio[i][j]["symbol"])
portfolio[i][j].update({"price" : symbol["price"]})
portfolio[i][j].update({"total" : symbol["price"] * portfolio[i][j]["shares"]})
gtotal = gtotal + portfolio[i][j]["total"]
# Get users balance
cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])
cash = cash[0]["cash"]
# Get total balance
total = cash + gtotal
# Pass: stocks, # shares, price per stock, total value of each holding, cash bal, g-total
return render_template("index.html", portfolio=portfolio, cash=cash, total=gtotal + cash)
*
render_template("index.html")
first thing. check50 will still fail, but if it does NOT give the Nonetype error, the problem is likely index. If it does give the error, the problem is likely buy.return redirect("/")
toredirect("index.html")
which has given me different log output. I'm now on :( buy handles valid purchase expected to find "112.00" in page, but it wasn't found Log sending POST request to /login sending POST request to /buy checking that "112.00" is in page Thanks for the tip :) Will post an answer when I crack it.