Flask-Bcrypt 运行 时间错误
Flask-Bcrypt Run time error
我正在使用 Flask 和 Bcrypt 完成一个简单的小用户登录。但是,当尝试使用存储在我的数据库中的用户登录时,我一直遇到 运行时错误。看起来像是某种 loop
坏掉了。由于我对编程还很陌生,因此要弄清楚我做错了什么非常具有挑战性。希望这里有人可以帮忙!我将通过下面的相关代码:
这是我遇到的问题。它只是不断循环直到超过最大值:
File "C:\Users\desktop\test\app\models.py", line 37, in password
return self.password
File "C:\Users\.virtualenvs\flask\lib\site-packages\sqlalchemy\ext\hybrid.py",
line 742, in get
return self.fget(instance)
File "C:\Users\desktop\test\app\models.py", line 37, in password
Models.py
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
nickname = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password = db.Column(db.String(100))
posts = db.relationship('Post', backref='author', lazy='dynamic',
primaryjoin="User.id==Post.user_id")
about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime)
followed = db.relationship('User',
secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref=db.backref('followers', lazy='dynamic'),
lazy='dynamic')
@hybrid_property
def password(self):
return self.password
@password.setter
def set_password(self, plaintext):
self.password = bcrypt.generate_password_hash(plaintext)
def is_correct_password(self, plaintext):
return bcrypt.check_password_hash(self.password, plaintext)
Forms.py
class LoginForm(Form):
nickname = TextField('Nickname', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
Views.py 登录
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(nickname=form.nickname.data).first_or_404()
if user.is_correct_password(form.password.data):
login_user(user)
flash("you've been logged in!, 'success'")
return redirect(url_for('index'))
else:
flash('your email or password doesnt match!', 'error')
return redirect(url_for('login'))
return render_template('login.html',
title='Sign In',
form=form)
Views.py 注册
@app.route('/register', methods=('GET', 'POST'))
def register():
form = forms.RegisterForm()
if form.validate_on_submit():
user = User(nickname=form.nickname.data, emai=form.email.data, password=form.password.data)
db.session.add(user)
db.session.commit()
flash('User successfully registered')
return redirect(url_for('login'))
return render_template('register.html', form=form)
您应该为列和 属性
使用不同的名称
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
password_hash = db.Column(db.String(100))
@hybrid_property
def password(self):
return self.password_hash
@password.setter
def set_password(self, plaintext):
self.password_hash = bcrypt.generate_password_hash(plaintext)
我正在使用 Flask 和 Bcrypt 完成一个简单的小用户登录。但是,当尝试使用存储在我的数据库中的用户登录时,我一直遇到 运行时错误。看起来像是某种 loop
坏掉了。由于我对编程还很陌生,因此要弄清楚我做错了什么非常具有挑战性。希望这里有人可以帮忙!我将通过下面的相关代码:
这是我遇到的问题。它只是不断循环直到超过最大值:
File "C:\Users\desktop\test\app\models.py", line 37, in password return self.password File "C:\Users\.virtualenvs\flask\lib\site-packages\sqlalchemy\ext\hybrid.py",
line 742, in get return self.fget(instance) File "C:\Users\desktop\test\app\models.py", line 37, in password
Models.py
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
nickname = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password = db.Column(db.String(100))
posts = db.relationship('Post', backref='author', lazy='dynamic',
primaryjoin="User.id==Post.user_id")
about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime)
followed = db.relationship('User',
secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref=db.backref('followers', lazy='dynamic'),
lazy='dynamic')
@hybrid_property
def password(self):
return self.password
@password.setter
def set_password(self, plaintext):
self.password = bcrypt.generate_password_hash(plaintext)
def is_correct_password(self, plaintext):
return bcrypt.check_password_hash(self.password, plaintext)
Forms.py
class LoginForm(Form):
nickname = TextField('Nickname', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
Views.py 登录
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(nickname=form.nickname.data).first_or_404()
if user.is_correct_password(form.password.data):
login_user(user)
flash("you've been logged in!, 'success'")
return redirect(url_for('index'))
else:
flash('your email or password doesnt match!', 'error')
return redirect(url_for('login'))
return render_template('login.html',
title='Sign In',
form=form)
Views.py 注册
@app.route('/register', methods=('GET', 'POST'))
def register():
form = forms.RegisterForm()
if form.validate_on_submit():
user = User(nickname=form.nickname.data, emai=form.email.data, password=form.password.data)
db.session.add(user)
db.session.commit()
flash('User successfully registered')
return redirect(url_for('login'))
return render_template('register.html', form=form)
您应该为列和 属性
使用不同的名称class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
password_hash = db.Column(db.String(100))
@hybrid_property
def password(self):
return self.password_hash
@password.setter
def set_password(self, plaintext):
self.password_hash = bcrypt.generate_password_hash(plaintext)