Here's my register function in application.py:

@app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""
    if request.method == "POST":
        username = request.form.get("username")
        password = request.form.get("password")
        confirmation = request.form.get("confirmation")

    if username == "":
        return apology("Missing username!")
    if password == "":
        return apology("Missing password!")
    if confirmation == "":
        return apology("Missing confirmation!")
    if password != confirmation:
        return apology("Password and confirmation don't match!")

    hash = generate_password_hash(password)

    result = db.execute("INSERT INTO users (username, hash) VALUES(?, ?)", username, hash)

    if not result:
        return apology("Username already exists.")

    rows = db.execute("SELECT id FROM users WHERE username = ?", username)
    #session["user_id"] = rows[0]["id"]

    return redirect("/")

    return render_template("register.html")

It passes most of the tests for the register function, except "registration rejects duplicate username" and "logging in as registered user succceeds." For some reason, when I uncomment the session["user_id"] = rows[0]["id"] part, it also doesn't pass the "registering user succeeds" test (but shouldn't that part also be included in the register function?). I've been stuck on this for days and can't figure out what could possibly be wrong...

  • Have you completed (or at least started) index? The supplied index function returns status 400 and is often the problem with these failures. Feb 6 at 16:20
  • Success on registering is tested by verifying that the / page was loaded successfully (because that is the final step in registering). As @DinoCoderSaurus mentioned, check50 will fail that test if you haven't completed the index function. (Also, if you haven't, you shouldn't be running check50 anyway, as it is designed to be a final test, not to test the functions piecemeal.
    – curiouskiwi
    Feb 7 at 5:15

You must log in to answer this question.

Browse other questions tagged .