0

I've completed register page html, and it works as expected. However, it won't insert the data from the user into the database. There are no errors. I can't seem to find how to debug python/flask setup. I tried searching and couldn't find anything helpful for the issue that I'm facing. Any help is much appreciated. I can also share my html if that will help, Here is my python code:

@app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""
    # Forget any user_id
    session.clear()

    # User reached route via POST (as by submitting a form via POST)
    if request.form.get == "POST":

        # Ensure username was submitted
        if not request.form.get("username"):
            return apology("must provide username", 403)

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

        # Query database for username
        check_username = db.execute("SELECT * FROM users WHERE username = :username",
                                    username=request.form.get("username"))

        # Ensure username does not exist in the DB
        if len(check_username) > 0:
            flash("Sorry, username is already taken!")
            return redirect("register.html", 403)

        else:
            # Add username, hash the password and add it to the DB 
            hashed_pass = generate_password_hash(request.form.get("password"))
            new_user = db.execute("INSERT INTO users ('username', 'hash') VALUES (:username, :new_pass)", username=request.form.get("username"), hash=hashed_pass)
            flash("Registration Successful!")
            session["user_id"] = new_user
            return redirect("login.html")

    else:
        return render_template("register.html")
1

Have you looked at the flask log (in the console where you flask run). I'd expect it to be giving an error hereabouts:

new_user = db.execute("INSERT INTO users ('username', 'hash') VALUES (:username, :new_pass)", username=request.form.get("username"), hash=hashed_pass)

It is expecting a value for the named parameter :new_pass, but the value is supplied with the name hash.

But wait there's more! It's never getting to the insert. If it was there would be an info message in flask log and/or it would have given an error on the name mismatch in the insert. (Cannot discern from the posted screen shot whether the sql syntax was corrected properly).

Look at this line:

if request.form.get == "POST":

Now look at the analogous line in login:

if request.method == "GET":

See the difference?

| improve this answer | |
  • Thank you @DinoCoderSaurus, I changed the values to a variable I declared in the previous line, no errors in the flask log console, please see my screenshot here: github.com/rajacs50/stackexchange/blob/master/flask.png really appreciate you helping me – rajacs50 Nov 4 '19 at 13:44
  • more info added to answer. – DinoCoderSaurus Nov 4 '19 at 20:40
  • Thank you very very much @DinoCoderSaurus !!! That was it. not only did you help me with the solution, you also taught me something! I appreciate you taking the time to help me. – rajacs50 Nov 5 '19 at 3:00

You must log in to answer this question.

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