0

I've been trying to figure out the register function for some time now, but I just can't get it to work.

I'm also receiving an invalid syntax error for the last line of the function: return redirect("/") - although this is exactly how the login function ends.

I feel as though I am repeating several request.for.get methods , however I'm not sure if I could reduce this by using variables. Here is my code below:

  @app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""
    # 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", 403)

        # Ensure username is not already taken
        rows = db.execute("SELECT * FROM users WHERE username = :username",
                          username=request.form.get("username"))
        if len(rows) == 1:
            return apology("username already taken. please choose another", 403)

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

        # Ensure password == confirmation
        elif request.form.get("password") != request.form.get("confirmation"):
            return apology("password and confirmation do not match", 403)

        # Add new user to the database
        session["user_id"] = db.execute("INSERT INTO users(username, hash) VALUES(:username, :hash)", username=request.form.get("username"), \
        hash=pwd_context.encrypt(request.form.get("password"))

    # Redirect user to home page
    return redirect("/")

Any help would be greatly appreciated!

1

You are missing the ) for your db.execute statement. Count the opening and closing parentheses.

3
  • Thanks for the catch. I fixed it , but still receiving the 500 server error – aherzfeld Jun 27 '18 at 5:28
  • Show the console output for the error. Edit your question, or maybe make it a new question. – Blauelf Jun 27 '18 at 6:26
  • I'm still having a problem with the register page. The error message has changed a bit - I've clarified the latest version of the problem here: cs50.stackexchange.com/questions/29371/… – aherzfeld Jun 27 '18 at 9:22

You must log in to answer this question.

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