0

I have coded the CS50 Finance problem set and it seems to be working fine from my end. However, check50 throws a 400 error for Post request on /register. Even in the console, I don't see that error.

Someone suggested to delete the data from the db. I tried to 'empty' the db but no luck. Infact for every new login which I make, my id does not start from 0 in the db. It remembers the last id no. and then increments that to my new row even after having emptied the db.

Would really appreciate some support on this!

My code for register is below:

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") #Password to have atleast of total 8 characters; with atleast 1 number and 1 out of @.#,* if len(password) < 6: return apology ("Please enter a password greater than 6 characters") b = password.isalpha() if b =='True': return apology("Password must contain atleast 1 number") if not '@' in password and not '#' in password and not '' in password: return apology(" Atleast 1 character of the following needs to be present: @,#,") count = 0 for a in password: if (a.isalpha()==True): count = count + 1 if count == 0: return apology("Atleast 1 alphabet needs to be present in the password") hash = generate_password_hash(password) #Checking if password and confirmation password are same if password != confirmation: return apology("Passwords don't match",403)

Query database for username

rows = db.execute("SELECT * FROM users WHERE username = ?", username)

Return apology if username exists

if len(rows) != 0 : return apology("Username already exists", 403) result = db.execute("INSERT INTO users(username, hash) VALUES (:username, :hash)", username=username, hash=hash) session["user_id"] = result

Redirect user to home page

return redirect("/") else: return render_template("register.html")

def index(): """Show portfolio of stocks""" result = db.execute("select * from portfolio") result2= db.execute("select cash from users where id=:id", id = session["user_id"]) cash= int(result2[0]['cash']) return render_template("index.html", portfolio = result, cash = cash

1 Answer 1

0

Which register test(s) fails? Does your register conform to the spec [emphasis added]:

Require that a user input a username, implemented as a text field whose name is username. Render an apology if the user’s input is blank or the username already exists.

Require that a user input a password, implemented as a text field whose name is password, and then that same password again, implemented as a text field whose name is confirmation. Render an apology if either input is blank or the passwords do not match.

You must log in to answer this question.

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