以 10 为底的 int() 的 Flask-sqlalchemy 无效文字:'None'

Flask-sqlalchemy invalid literal for int() with base 10: 'None'

所以我创建了一个 db.model 并使用 Flask-Login 创建了一个登录系统,一切似乎都正常工作,现在突然出现“ValueError: invalid literal for int()以 10 为基数:'None'”。显然一些实际上什么都不包含的东西正在传递,但我看不到什么和在哪里。谁能帮忙? 这是我突出显示的代码片段,据说会导致我的问题:

return Users.query.get(int(user_id))

return render_template('Index.html', New_User=All_Users)

这两个是以下代码片段的一部分:

@login_manager.user_loader
def load_user(user_id):
     return Users.query.get(int(user_id))


def Index():
if request.method=='POST':
    New_Username = request.form['Username']
    New_Password = request.form['Password']
    user = Users.query.filter_by(Username=New_Username).first()
    if not user:
        New_User = Users(Username=New_Username, Password=New_Password)
        db.session.add(New_User)
        db.session.commit()
        login_user(New_User)
        return redirect('/Dashboard')
    else:
        return render_template('Index.html', New_User=All_Users)
else:
        return render_template('Index.html', New_User=All_Users)

整个回溯:

File "E:\Python\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)

File "E:\Python\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "E:\Python\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "E:\Python\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "E:\Python\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "E:\Python\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "E:\Python\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "E:\Python\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "E:\Python\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "E:\Python\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "run.py", line 56, in Index
return render_template('Index.html', New_User=All_Users)
File "E:\Python\Lib\site-packages\flask\templating.py", line 136, in render_template
ctx.app.update_template_context(context)
File "E:\Python\Lib\site-packages\flask\app.py", line 838, in update_template_context
context.update(func())
File "E:\Python\Lib\site-packages\flask_login\utils.py", line 379, in _user_context_processor
return dict(current_user=_get_user())
File "E:\Python\Lib\site-packages\flask_login\utils.py", line 346, in _get_user
current_app.login_manager._load_user()
File "E:\Python\Lib\site-packages\flask_login\login_manager.py", line 318, in _load_user
user = self._user_callback(user_id)
File "run.py", line 30, in load_user
    def __repr__(self):
        return f'{self.Username}'
 
@login_manager.user_loader
def load_user(user_id):
    return Users.query.get(int(user_id))
 
 
 
@app.route('/Logout')
@login_required

当您在登录时查询 Users 模型时,您的 user_loader returns 用户将访问您的 '/login' 视图函数,或调用 login_user()。当您查询数据库时,如果出现 404(因此未找到用户),则返回 None。您已尝试使用 if not user:... 来处理此问题,但您应该改为尝试 if user is Noneif user==None,甚至将错误视为默认值并说 if user is not None: #code for logging in。然后您的视图函数将处理从查询返回 None 的数据库。