I'm trying to code the register function for 2017 pset7. The proper screen comes up, and the program behaves correctly for missing username, missing password, incorrectly repeated passwords, and username already taken. It does not seem to register new users. No error code is thrown. I had a tester print statement immediately after the last line of code posted here, and that was triggered, so I know that my last statement is being executed. But... I can't log in after registering someone, and nothing changes on the phpLiteAdmin page after registering someone. And after registering someone, I do not get a 'username already taken' message when I try to re-register that same name. I'm stumped. Thank you for any help!

@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user."""

# forget any user_id

# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":

    # ensure username was submitted
    if not request.form.get("username"):
        return apology("must provide username")

    # ensure password was submitted
    elif not request.form.get("password"):
        return apology("must provide password")

    # ensure password was submitted second time
    elif not request.form.get("confirm_password"):
        return apology("must confirm password")

    # ensure both passwords entered match    
    elif request.form.get("password") != request.form.get("confirm_password"):
        return apology("The passwords entered do not match")

    # query database for username   
    rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
    if rows:
        return apology("That username already exists")

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

Well... figured it out. I forgot to hash the new password. So I added...

hashed = pwd_context.encrypt(request.form.get("password"))

and now it works. Duh.

You must log in to answer this question.

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