简体   繁体   中英

insering the hashed password into sqlite3 table

[ terminaldb_table 上的错误消息

Hi I'm trying to insert a hashed password using;

werkzeug.security.generate_password_hash(password, method='pbkdf2:sha256', salt_length=8)

but I'm getting internal server errors because of the line I've highlighted on the first image of the error screenshot.

I'm using CS50 IDE, python, sqlite3, werkerzeug security library

TIA

Here my code for the route:

@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.method == "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"):
            return apology("must provide password", 403)

        # Ensure password was submitted
        elif not request.form.get("password2"):
            return apology("must re-type password", 403)

        # Check the password match
        if request.form.get("password") != request.form.get("password2"):
            return apology("your password didn't match")

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

        # Ensure username not exists
        if len(rows) == 1:
            return apology("username exists, please select another")

        # Otherwise insert the user to the users TABLE
        else:
            hashed_pass = generate_password_hash((request.form.get("password")), method='pbkdf2:sha256', salt_length=len(request.form.get("password")))
            db.execute("INSERT INTO users (username, hash) VALUES (:username, ?)",
                        username=request.form.get("username"), hash=hashed_pass)

        # Return apology if cannot register
        # Query database for username to check
        rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))

        if len(rows) != 1:
            return apology("Sorry something went wrong, please try again")

        # If register succesful
        else:
            return redirect("/login")

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

Your insert into users should look like

sql = "INSERT INTO users (username, hash) VALUES (?, ?)"
values = (request.form.get("username"), hashed_pass)
db.execute.execute(sql, values)

OR

db.execute.execute("INSERT INTO users (username, hash) VALUES (?, ?)", (request.form.get("username"), hashed_pass))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM