So I'm new to programming, currently taking the HarvardX CS50 course. So forgive my noob-level understanding of things.
Right now I'm working on a registration page for a website. User needs to enter a username and password, then confirm their password. The system should then check to make sure that the username is not already in use. This is where my problem lies I believe:
# Checks to see if username exists already, if so return apology
usernamecheck = db.execute("SELECT COUNT(*) FROM users WHERE username = :username", username)
if usernamecheck != 0:
return apology("Username already exists.")
I've been sitting with this for days and can't crack it. Would greatly appreciate someone telling me how to fix this.
Code:
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
if request.method == "GET":
return render_template("register.html")
else:
# Take input for username
username = request.form.get("username")
#If username is blank return apology
if not username:
return apology("You must enter a username.")
# Checks to see if username exists already, if so return apology
usernamecheck = db.execute("SELECT COUNT(*) FROM users WHERE username = :username", username)
if usernamecheck != 0:
return apology("Username already exists.")
# Take input for password
password = request.form.get("password")
# if password is blank, return apology
if not password:
return apology("You must enter a password.")
# Take input for confirmation
confirmation = request.form.get("confirmation")
# if confirmation is blank, return apology
if not confirmation:
return apology("You must confirm your password.")
# if password and confirmation don't match, return apology
if password != confirmation:
return apology("Your password confirmation does not match the password.")
# hash the password with generate_password_hash
pwhash = generate_password_hash(request.form.get("password"))
# insert the username and hash into the users table
sql = "INSERT INTO users (username, hash) VALUES (':username', '1234')"
# sql = "INSERT INTO users (username, pwhash VALUES ('" + username + "', '" + pwhash + "'), username = '" + username + "', pwhash = '" + pwhash + "'"
print(sql)
db.execute(sql)
return redirect("/")
Here is what terminal shows for the error:
ERROR:application:Exception on /register [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/finance/application.py", line 160, in register
usernamecheck = db.execute("SELECT COUNT(*) FROM users WHERE username = :username", username)
File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 21, in decorator
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 219, in execute
raise RuntimeError("missing value for placeholder (:{})".format(name))
RuntimeError: missing value for placeholder (:username)
INFO:werkzeug:192.168.83.137 - - [27/Jun/2020 02:51:17] "POST /register HTTP/1.0" 500 -
INFO:werkzeug:192.168.83.137 - - [27/Jun/2020 02:51:17] "GET /static/styles.css HTTP/1.0" 200
Edit: I've been through the Python documentation, I see their examples, but trying to implement it doesn't work for some reason. I've been at this for days and I'm getting super frustrated. Hoping someone can provide not just the solution but also explain it in a relatively easy to understand way?