You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask-Bcrypt的check_password_hash在Render部署环境始终返回False

Flask认证部署PostgreSQL后check_password_hash始终返回False的问题

我开发了一个简单的Flask认证应用,本地使用MySQL时运行正常,但部署在Render平台搭配PostgreSQL服务后,check_password_hash方法始终返回False。本地代码运行无问题,部署后功能失效,已尝试更换为Werkzeug并核对部署环境的相关版本,以下是相关代码片段:

注册视图

# Registration route
@app.route("/register", methods=["GET", "POST"])
def register():
    if request.method == "POST":
        username = request.form["username"]
        # print(username)-debug
        password = request.form["password"]
        # bytes_register = password.encode("utf-8")
        # print(password)-debug

        # create_table_query_mysql = """
        #     CREATE TABLE IF NOT EXISTS users (
        #         id SMALLINT(5) AUTO_INCREMENT PRIMARY KEY,
        #         username CHAR(128) NOT NULL UNIQUE,
        #         pass_hash CHAR(128) NOT NULL
        #     )
        # """

        create_table_query_postgresql = """
            CREATE TABLE IF NOT EXISTS users (
                id SERIAL PRIMARY KEY,
                username CHAR(255) NOT NULL UNIQUE,
                password_hash CHAR(255) NOT NULL
            )
        """

        existing_user_query = "SELECT * FROM users WHERE username=%s"
        existing_user_username = None

        cursor = connection_db.cursor()
        cursor.execute(create_table_query_postgresql)

        cursor.execute(existing_user_query, (username,))
        existing_user_username = cursor.fetchone()

        if existing_user_username:
            error_message = "Username taken. Please choose a different username"
            return render_template("register.html", error_message=error_message)

        hashed_password = bcrypt.generate_password_hash(password)
        decoded_hashd_password = hashed_password.decode("utf-8")

        insert_user_query = (
            "INSERT INTO users (username, password_hash) VALUES (%s, %s)"
        )
        cursor.execute(insert_user_query, (username, decoded_hashd_password))
        connection_db.commit()
        return render_template("registration_success.html")

    return render_template("register.html")

登录视图

# Login Route
@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        username_login = request.form["username"]
        password_login = request.form["password"]
        # bytes_login = password_login.encode("utf-8")

        # create_table_query_mysql = """
        #     CREATE TABLE IF NOT EXISTS users (
        #         id SMALLINT(5) AUTO_INCREMENT PRIMARY KEY,
        #         username CHAR(128) NOT NULL UNIQUE,
        #         pass_hash CHAR(128) NOT NULL
        #     )
        # """

        create_table_query_postgresql = """
            CREATE TABLE IF NOT EXISTS users (
                id SERIAL PRIMARY KEY,
                username CHAR(255) NOT NULL UNIQUE,
                password_hash CHAR(255) NOT NULL
            )
        """

        user_query = "SELECT * FROM users WHERE username=%s"
        user_data = None

        cursor = connection_db.cursor()
        cursor.execute(create_table_query_postgresql)
        cursor.execute(user_query, (username_login,))
        user_data = cursor.fetchone()
        hash = user_data[2]
        print(hash)
        print(password_login)
        # hash_bytes = hash.encode("utf-8")
        # print(hash_bytes)
        check = bcrypt.check_password_hash(hash, password_login)
        print(check)

        if user_data and check:
            user = User(id=user_data[0], username=user_data[1])
            login_user(user)
            return redirect("/dashboard")
        else:
            not_valid = "Invalid User"
            return render_template("login.html", not_valid=not_valid)

    return render_template("login.html")

出错代码片段

cursor.execute(user_query, (username_login,))
user_data = cursor.fetchone()
hash = user_data[2]
print(hash)
print(password_login)
# hash_bytes = hash.encode("utf-8")
# print(hash_bytes)
check = bcrypt.check_password_hash(hash, password_login)
print(check)

内容的提问来源于stack exchange,提问作者Adnan Bayadwala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 17:17:48