我仍处于学习阶段,在学习教程时遇到此问题
我不明白为什么我会收到此错误 ValueError: Invalid salt
完整的错误代码是:
<块引用>文件“C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py”, 第 1515 行,在 full_dispatch_request 中 rv = self.handle_user_exception(e) 文件“C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py”, 第 1513 行,在 full_dispatch_request 中 rv = self.dispatch_request() 文件“C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py”, 第 1499 行,在 dispatch_request 中 返回 self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) 文件“C:\Users\Aparichit\Desktop\NoViewsIndia\noViews\views.py”,第 46 行,在 login_page 如果attempted_user 和bcrypt.check_password_hash(attempted_user.password_hash, 尝试密码): 文件“C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask_bcrypt.py”, 第 193 行,在 check_password_hash 中 return safe_str_cmp(bcrypt.hashpw(password, pw_hash), pw_hash) 文件“C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\bcrypt_init_.py”, 第 105 行,在 hashpw 中 raise ValueError("Invalid salt")
我的 model.py
代码是:
class User(db.Model, UserMixin):
name = db.Column(db.String(20), nullable=False)
userName = db.Column(db.String(15), primary_key=True, nullable=False)
password_hash = db.Column(db.String(20), nullable=False)
@property
def password(self):
return self.password
@password.setter
def password(self, plain_text_password):
self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8')
而 views.py
是
@app.route('/admin_login', methods=['GET', 'POST'])
def login_page():
login_form =Login()
if login_form.validate_on_submit():
attempted_password = login_form.pWord.data
attempted_user = User.query.filter_by(userName = login_form.uName.data).first()
print(f'Route Username is {login_form.uName.data}')
print(f'Route User is {attempted_user}')
print(f' Route Password is {attempted_password}')
print(f'Route Hashed Password is {attempted_user.password_hash}')
if attempted_user and bcrypt.check_password_hash(attempted_user.password_hash, attempted_password):
login_user(attempted_user)
flash(f'You have successfully logged in, {attempted_user.name}')
else:
flash('Invalid Username and Password')
return render_template('admin_login.html', loginForm=login_form)
而 form.py
是
class Login(FlaskForm):
uName = StringField(label='User Name')
pWord = PasswordField(label='Password')
submit = SubmitField(label='Login')
以及保存的密码: Hassed Password
答案 0 :(得分:0)