Flask SQLAlchemy,密码不存储在数据库中

huangapple go评论61阅读模式
英文:

Flask SQLalchemy, password is not stored in database

问题

@app.route("/register", methods=["GET", "POST"])
def register():
    # 连接数据库到flask

    # name = username
    username = request.form.get("username")
    if request.method == "POST":
        # 确保输入了用户名
        if not username:
            flash("必须输入用户名")
            return render_template("register.html")
        # 确保输入了密码
        elif not request.form.get("password"):
            flash("必须输入密码")
            return render_template("register.html")

        # 确保确认了密码
        elif not request.form.get("confirmation"):
            flash("必须确认密码")
            return render_template("register.html")
        # 确保输入了收入
        elif not request.form.get("income"):
            flash("必须输入月收入才能继续")
            return render_template("register.html")

        # 确保密码和确认密码匹配
        elif request.form.get("password") != request.form.get("confirmation"):
            flash("密码不匹配")
            return render_template("register.html")

        # 查询数据库以查看用户名是否已被使用
        founduser = users.query.filter_by(username=username).first()

        # 对密码进行哈希处理
        _hash = generate_password_hash(request.form.get("password"))
        income = request.form.get("income")
        if founduser:
            flash("用户名已被使用")
            return render_template("register.html")
        else:
            registereduser = users(username, _hash, income)
            db.session.add(registereduser)
            db.session.commit()
            _id = registereduser._id
        session["user_id"] = _id
        flash("注册成功!")
        return redirect("/home")

    else:
        return render_template("register.html")
class users(db.Model):
    __tablename__ = "users"

    _id = db.Column("id", db.Integer, primary_key=True, autoincrement=True)
    username = db.Column("username", db.String(100))
    _hash = db.Column("hash", db.String(100))
    income = db.Column("income", db.Float)

    def __init__(self, username, _hash, income):
        self.username = username
        self._hash = _hash
        self.income = income
        return f"<User: {self.username}"
英文:

I am trying to store users in a database when they register to my flask website using SQL alchemy, the username and income stores correctly but the hashed password does not appear in the database and I don't know why. My register function is here:

def register():
    # connect database to flask

    # name = username
    username = request.form.get(&quot;username&quot;)
    if request.method == &quot;POST&quot;:
        # ensure username is entered
        if not username:
            flash(&quot;must enter username&quot;)
            return render_template(&quot;register.html&quot;)
        # ensure password is entered
        elif not request.form.get(&quot;password&quot;):
            flash(&quot;must enter password&quot;)
            return render_template(&quot;register.html&quot;)

        # ensure password has been confirmed
        elif not request.form.get(&quot;confirmation&quot;):
            flash(&quot;must confirm password&quot;)
            return render_template(&quot;register.html&quot;)
        # ensure income has been entered
        elif not request.form.get(&quot;income&quot;):
            flash(&quot;must enter Monthly income to proceed&quot;)
            return render_template(&quot;register.html&quot;)

        # ensure password and confirmation password match
        elif request.form.get(&quot;password&quot;) != request.form.get(&quot;confirmation&quot;):
            flash(&quot;passwords do not match&quot;)
            return render_template(&quot;register.html&quot;)

        # query database to see if username is already taken
        founduser = users.query.filter_by(username=username).first()

        # hash password
        _hash = generate_password_hash(request.form.get(&quot;password&quot;))
        income = request.form.get(&quot;income&quot;)
        if founduser:
            flash(&quot;username is already taken&quot;)
            return render_template(&quot;register.html&quot;)
        else:
            registereduser = users(username, _hash, income)
            db.session.add(registereduser)
            db.session.commit()
            _id = registereduser._id
        session[&quot;user_id&quot;] = _id
        flash(&quot;registered successfully!&quot;)
        return redirect(&quot;/home&quot;)

    else:
        return render_template(&quot;register.html&quot;)

the user table is here:

class users(db.Model):
    __tablename__ = &quot;users&quot;

    _id = db.Column(&quot;id&quot;, db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(&quot;username&quot;, db.String(100))
    _hash = db.Column(&quot;hash&quot;, db.String(100))
    income = db.Column(&quot;income&quot;, db.Float)

    def __init__(self, username, _hash, income):
        self.username = username
        self._hash = _hash
        self.income = income
        return f&quot;&lt;User: {self.username}&quot;

答案1

得分: 0

我没有测试,但我相信这只是一个拼写错误:

self.hash = _hash

根据你的模型,应该是 self._hash = _hash

英文:

I didn't test, but i believe it is just a typo issue:

self.hash = _hash

According to your model , it should be self._hash = _hash

huangapple
  • 本文由 发表于 2023年3月31日 21:22:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定