英文:
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("username")
if request.method == "POST":
# ensure username is entered
if not username:
flash("must enter username")
return render_template("register.html")
# ensure password is entered
elif not request.form.get("password"):
flash("must enter password")
return render_template("register.html")
# ensure password has been confirmed
elif not request.form.get("confirmation"):
flash("must confirm password")
return render_template("register.html")
# ensure income has been entered
elif not request.form.get("income"):
flash("must enter Monthly income to proceed")
return render_template("register.html")
# ensure password and confirmation password match
elif request.form.get("password") != request.form.get("confirmation"):
flash("passwords do not match")
return render_template("register.html")
# 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("password"))
income = request.form.get("income")
if founduser:
flash("username is already taken")
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("registered successfully!")
return redirect("/home")
else:
return render_template("register.html")
the user table is here:
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}"
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论