0

in pset7, I'm using the date function to store the time of buying, I did some Google search but I keep having this error:undefined variable'date', could someone help me figure out what's wrong?

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

    #if it's blank
    if not request.form.get("symbol"):
        return apology("You must enter a symbol")

    symbol=request.form.get("symbol")
    num=int(request.form.get("number")) 
    if symbol.isalpha()== False:
        return apology("Make sure you enetered a valid symbol!")
    elif num <=0:
        return apology("Please enter a positive number!")

    #get the stock info from lookup function
    result=lookup(request.form.get("symbol"))

    #can user afford that stock?
    total=result["price"] * num

    cash=dp.execute("SELECT cash from users WHERE id=:id ",session["user_id"] )
    if(cash<=total):
        return apology("make sure you have enough cash for this stock")
    else:
        dp.execute("INSERT INTO portfolio (stockSymbol, howMany, price,total price, when) VALUES (:id, :stock, :howMany, :price, :total, :when "
        ,session["user_id"], result["name"], num, result["price"] ,total, date('now'))

#if you reached the page(route) via a GET request    
else:
    return render_template("buy.html")
0

date('now') is a sqlite function. db.execute is a python function. So there is no date('now') in python. Instead of using the bound variable :when, you can simply use date('now') (or datetime('now') to get time) in the VALUES clause. (The VALUES clause also seems to be missing a closing )).

1
  • I'm having the same problem. I tried the above solution but got the following error code: NameError: name 'date' is not defined
    – KitSeason
    Sep 14 '17 at 23:20

You must log in to answer this question.

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