after I run check50 I get following comments:
Below are my code for register:
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
session.clear()
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")
elif not request.form.get("confirmation"):
return apology("must confirm your password")
elif request.form.get("passowrd") != request.form.get("confirmation"):
return apology("password don't match")
# Add username and password to database
rows = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=request.form.get("username"), hash=generate_password_hash(request.form.get("password")))
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=request.form.get("username"))
if len(rows) != 1:
return apology("username already in use")
session["user_id"] = rows[0]["id"]
return redirect("/")
else:
return render_template("register.html")
I've completed all function but since the register function does not work, I could not check the correctness of my other functions. I could not figure out what's wrong with my code. Any suggestion would be much appreciated. Thank you!
Updated Code:
result = db.execute("INSERT INTO users (username, hash) VALUES(:username,
:hash)", username=request.form.get("username"),
hash=generate_password_hash(request.form.get("password")))
if not result:
return apology("username already in use")
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=request.form.get("username"))
session["user_id"] = rows[0]["id"]
return redirect("/")
else:
return render_template("register.html")