I've hit a snag when trying to implement register in pset7. Every time I enter a new username, password, and password confirmation, I'm getting a 500 server error. I don't know how to read the log to understand what is creating this error in my code.

I have already looked at a similar question here, but found the answer did not apply to my problem, and I do not have check50 for this problem in the 2018 version of CS50x: How can I debug an internal server error in pset 7 to get past login and register?

Any insight would be appreciated. Thank you for your time!

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

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

    elif not request.form.get("confirmation"):
        return apology("please confirm your password", 403)

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

    # if all conditions met, store username

    # hash password
    hash = generate_password_hash(request.form.get("password"), method='pbkdf2:sha256', salt_length=8)

    """Register user"""
    # check to see if that username already exists in users column
    result=db.execute("SELECT * FROM users WHERE username = :username")

    # if user name already exists db.execute fails, so return apology
    if not result:
        return apology("sorry, username already exists", 403)

    # insert username and hashed password, AND create a user_id in session[] to log in
    session["user_id"]=db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",
    username=request.form.get("username"), hash=hash)

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

# User reached route via GET (by clicking a link or via redirect)
    return render_template("register.html")

Log 1 Log 2

1 Answer 1


The last line in the log about errorhandler is a red herring. More on that later; will edit after proper research.

The important line in the log is where it says something about bind parameters (if log was posted as text not image I could cite it here).

This result=db.execute("SELECT * FROM users WHERE username = :username") is not proper syntax.

When bind parameters are used in an execute, more arguments are required, specifically, one "set" for each bind parameter. Have a look in the login function to see how the distro code calls execute with a bound parameter. Verify that all the sqls that use bound parameters use the proper syntax in execute call.

Some changes to application.py will prevent the error messages from errorhandler.

  • Add InternalServerError to the import exceptions line like so:
    from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
  • Add two lines the the errorhandler function like so:

    def errorhandler(e): """Handle error""" if not isinstance(e, HTTPException): e = InternalServerError() return apology(e.name, e.code)

  • You're right about that line. But... Umm... the parameters are on the next line, the parentheses from db.execute( are closed there, so that's valid syntax from the text. I'd guess request.form.get("username") is giving wrong value, maybe form field is named the wrong way. Funny we get there at all after the user-exists-check is inverted.
    – Blauelf
    Feb 27, 2019 at 9:00
  • Thanks @Blauelf. Answer corrected to indicate the right wrong line. Feb 27, 2019 at 12:25
  • Thanks for your help, DinoCoderSaurus and @BlauElf. I changed my code for result=db.execute, and edited the way I was checking for existing user names, and that seemed to do the trick. Something else did come up: the registration worked, but I have an error popping up in my log that says ""GET / HTTP/1.1" 400," plus I get an error message in my web browser. Do you know what this could be? I can also post this as another question.
    – soonthoj
    Feb 27, 2019 at 17:40
  • @DinoCoderSaurus Ah, now I see that, too :D Was totally on the wrong train myself.
    – Blauelf
    Feb 27, 2019 at 17:57
  • 1
    Looks like a new question would be good then. But "TODO" might be a function you are to implement, maybe index.
    – Blauelf
    Feb 28, 2019 at 6:25

You must log in to answer this question.

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