2

In the latest CS50 - pset7 - CS50 Finance, it is advised to use generate_password_hash instead of pwd_context.encrypt.

Also, in the template, there is a change where instead of url_for('register'), /register" is used.

I am unable to register and keep getting apology message. I am unable to register for an account and log in (despite login and logout already working)! And I am unable to see my rows via phpLiteAdmin or sqlite3.

Are we suppose to register only after implementation of all the functions or am I missing something? Thanks!

.......................................................

REGISTER

    def register():
    """Register user"""

    # User reached route via POST (as by submitting a form via POST)
    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")

        # Ensure password confirmation was submitted
        elif not request.form.get("password_confirmation"):
            return apology("must provide password confirmation")

        # Ensure passwords are matching
        elif request.form.get("password") != request.form.get("password_confirmation"):
            return apology("Sorry, password didn't match. Try Again!")

        # Hash password / Store password hash_password =
        hashed_password = generate_password_hash(request.form.get("password"))

        # Add user to database
        result = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",
                username = request.form.get("usermame"),
                hash = hashed_password)

        if not result:
            return apology("The username is already taken")

        rows = db.execute("SELECT * FROM users WHERE username = :username",
                  username = request.form.get("username"))


        # Remember which user has logged in
        session["user_id"] = rows[0]["id"]

        # Redirect user to home page
        return redirect("/")

    # User reached route via GET (as by clicking a link or via redirect)
    else:
        return render_template("register.html")

..........................................................

My template (register.html):

<form action="/register" method="post">
    <div class="form-group">
        <input autocomplete="off" autofocus class="form-control" 
 name="username" placeholder="Username" type="text"/>
    </div>
    <div class="form-group">
        <input class="form-control" name="password" placeholder="Password" 
 type="password"/>
    </div>
    <div class="form-group">
        <input class="form-control" name="password_confirmation" 
 placeholder="Password (again)" type="password"/>
    </div>
    <button class="btn btn-primary" type="submit">Register</button>
</form>

...................................................................

1 Answer 1

0
  • Found the error, spelled "username" as "usermame" while defining results and adding user to the database.

You must log in to answer this question.

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