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."""
session.clear()
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"))
else:
return render_template("login.html")