As the title says, I am working on the register method of cs50 finance. When I click on the register tab of the site, my login template seems to be popping up instead. I tried returning the register template near the start of the register method, but that didn't fix anything. The problem only goes away when I get rid of my "POST conditional," but even then, a user is not actually registered in the database. I'm fairly sure my code is correct; I followed every step in the walkthrough and it makes logical sense. I'm not sure what I'm doing wrong. Could someone please help point my error out?

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

    if request.method == "POST": #POST CONDITIONAL

        return render_template("register.html") # TRIED RETURNING TEMPLATE AT START

        if not request.form.get("username"):
            return apology("must provide username")

        elif not request.form.get("password"):
            return apology("must provide password")

        elif not request.form.get("password_comfirm"):
            return apology("must confirm password")

        elif request.form.get("password") != request.form.get("password_confirm"):
            return apology("password must match password confirmation")

        hash = pwd_context.encrypt("password")

        result = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=request.form.get("username"), hash=hash)
        if not result:
            return apology("username is already taken")

        session["user_id"] = result[0]["id"]

        return redirect(url_for("index"))

        return render_template("login.html")

2 Answers 2


The post method is only used when you submit a form. Clicking register doesn't count as a post method, therefore you are skipping the entire section of your code in the post conditional, and the code reads the else statement.


First, I'll explain my main error. My problem was that I returned register.html in the body instead of returning it as the else statement following the POST conditional. I had other errors in the code, namely hashing the password and signing in the user, but I won't reveal how I solved those ;)

You must log in to answer this question.

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