I ran a check50 and i found that its not checking if the user is in the database when registering so i decided to put a if statement which checks if the user is not added but it gives an error and i am not sure what it really means
this is the error i get:
ERROR: Exception on /register [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/pset9/finance/application.py", line 191, in register
added_user = db.execute("INSERT INTO users (username, hash) VALUES(:username, :password_hash)", username=username, password_hash=password_hash)
File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 21, in decorator
return f(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/cs50/sql.py", line 378, in execute
raise e
ValueError: UNIQUE constraint failed: users.username
this is my code:
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
if not(username):
return apology("Oops you need a username!")
elif not(password) or not(confirmation):
return apology("Oops you need a password!")
elif password != confirmation:
return apology("Oops password and confirmation password do not match")
password_hash = generate_password_hash(password)
added_user = db.execute("INSERT INTO users (username, hash) VALUES(:username, :password_hash)", username=username, password_hash=password_hash)
if not added_user:
return apology("username is already registered")
session["user_id"] = added_user
return redirect(url_for('index'))
else:
return render_template("register.html")