【问题标题】:insering the hashed password into sqlite3 table将散列密码插入 sqlite3 表
【发布时间】:2020-12-06 15:27:18
【问题描述】:

[

您好,我正在尝试使用以下方法插入散列密码;

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

但由于我在错误屏幕截图的第一张图片上突出显示的那行,我遇到了内部服务器错误。

我正在使用 CS50 IDE、python、sqlite3、werkerzeug 安全库

TIA

这是我的路线代码:

@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")

【问题讨论】:

  • 参数样式不一致。不应该是这样的:INSERT INTO users (username, hash) VALUES (:username, :hash) 吗?

标签: python sqlite werkzeug


【解决方案1】:

您插入用户的内容应该是这样的

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

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

【讨论】:

  • 我都试过了,也试过把 % 改成 ?作为值占位符,不幸的是,该行仍然出现内部错误
  • @cetins 我相信这次的错误不一样 - 不是吗?
  • 我不知道很抱歉,只看到“错误 - 寄存器中的第 156 行”。但是我已经改变了 cmets INSERT INTO users (username, hash) VALUES (:username, :hash) 中的行,现在它可以工作了
猜你喜欢
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 2017-10-20
  • 2016-09-20
  • 2011-02-18
  • 2017-07-11
相关资源
最近更新 更多