0

I am working on the Finance PSET8 and am having trouble with the Register part of the problem.

This is my application.py code:

    @app.route("/register", methods=["GET", "POST"])
    def register():
        session.clear()
    
        if request.method == "GET":
            return render_template("register.html")
    
        else:
            # Checking for empty username field
            if not request.form.get("username"):
                return apology("Please provide Username", 403)
    
            # checking for emopty password field
            elif not request.form.get("password"):
                return apology("Please provide a password", 403)
    
            # checking for empty confirmation field
            elif not request.form.get("confirmation"):
                return apology("Please confirm your password", 403)
    
            elif request.form.get("password") != request.form.get("confirmation"):
                return apology("Passwords do not match", 403)
    
            username = db.execute("SELECT username FROM users WHERE username = :username",
                username = request.form.get("username"))
    
            if len(username == 1):
                return apology("Username Taken", 403)
    
            else:
                #hash password
                pw_hash = generate_password_hash(request.form.get("password"))
    
                new_user = db.execute("INSERT INTO users (username, hash) VALUES (:username, :password)",
                    username = username,
                    password = pw_hash)
    
                session["user_id"] = new_user
    
                username = request.form.get("username")
                flash(f"Registered as {username}")
    
                return redirect("/")

I can run flask, and get the server going, but then end up with a 500 error whenever I try to register a new user.

Any help would be great, thanks

Ed

1
  • Have a look in the flask console to see the traceback. It will point you to the error, and perhaps you'll see the problem. If not, edit the traceback into the question. It might complain about this if len(username == 1): – DinoCoderSaurus Dec 10 '20 at 15:46
1
File "…/finance/application.py", line 266, in register
    if len(username == 1):
TypeError: object of type 'bool' has no len()

Assuming that db.execute() returns a list of rows, you could change if len(username == 1): to simply if username:, although I'd probably rename the variable anyway since "username" doesn't describe its contents.

You must log in to answer this question.

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