0

I've implemented the following code in my "/register" route:

def register():
    """Register user"""
    if request.method == "GET":
        return render_template("register.html")
    else:
        # Check if username field blank
        username = request.form.get("username")
        if not username:
            return apology("Enter a username")

        # Check is username already taken
        rows = db.execute("SELECT * FROM users WHERE username = :username", username=username)
        if len(rows != 0):
            return apology("Username already taken")

        # Check if password field blank
        password = request.form.get("password")
        if not password:
            return apology("Enter a password")

        # hash the inputted password
        hashedpass = generate_password_hash(password)

        # store the hashed password
        db.execute("INSERT INTO users (username, hash) VALUES (:username, :hashedpass)", username=username, hashedpass=hashedpass)

        return redirect("/login")

However when I try to test it and register as a user, the following error occurs:

raceback (most recent call last):
  File "/Users/silicon/anaconda3/envs/cs50/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/silicon/anaconda3/envs/cs50/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/silicon/anaconda3/envs/cs50/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/silicon/anaconda3/envs/cs50/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/silicon/anaconda3/envs/cs50/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/silicon/anaconda3/envs/cs50/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/silicon/Documents/Code/pset8/finance/application.py", line 131, in register
    if len(rows != 0):
TypeError: object of type 'bool' has no len()

Could anyone explain why this does not work while the code below, which is provided as distribution code as part of the "/login" route works?

        # Query database for username
        rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))

        # Ensure username exists and password is correct
        if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
            return apology("invalid username and/or password", 403)
1

Move your != 0 outside that parenthesis, len(rows) != 0:

| improve this answer | |
  • Ah, that was a stupid mistake. Thanks! – Jason C May 20 at 23:21
  • It's ok it happens, also just an advice you can remove your else in your first if function, for good practice, so you don't keep indenting and indenting. because at your first if you return a template after a return in a function the code after it doesn't run anymore so you don't need to put it in an else.. – Ojou Nii May 21 at 1:39

You must log in to answer this question.

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