1

File "/home/ubuntu/workspace/pset7/finance/application.py", line 267, in register session["user_id"] = result[0]["id"] TypeError: 'int' object is not subscriptable

if request.method == "POST":

    if not (request.form.get("password") or request.form.get("confirmation") or request.form.get("username")):
        return apology("please enter all details", 403)



    if request.form.get("confirmation") != request.form.get("password"):
        return apology("entered passwords don't match", 403)

result = db.execute("INSERT INTO users (username, hash) \ VALUES(:username, :hash)", \ username=request.form.get("username"), \ hash=generate_password_hash(request.form.get("password")))

    if not result :
        return apology("username already taken", 403)


    #Remember which user has logged in
    session["user_id"] = result[0]["id"]

    return redirect("/index")
else:
    return render_template("register.html")

1 Answer 1

1

Per the spec hints:

If str is an INSERT, and the table into which data was inserted contains an autoincrementing PRIMARY KEY, then execute returns the value of the newly inserted row’s primary key.

So your result is not a dictionary, it's an integer (the value of the primary key). You can simply store result directly.

2
  • ok so are you saying that i cant use the nt operator since it only operates on objects and since result is an int i should use this:if result > 0 : return apology("username already taken", 403).
    – user21692
    Commented Jul 11, 2018 at 3:10
  • No, I was saying that you can't access result[0]["id"] when setting the session id.
    – curiouskiwi
    Commented Jul 12, 2018 at 3:24

You must log in to answer this question.

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