随机时间没有user_loader异常(在重置时消失)

时间:2018-12-11 20:12:12

标签: flask flask-login

我以调试模式运行flask,并且经常进行更改并重新加载页面时,会抛出No user_loader异常

  

异常:此LoginManager尚未安装user_loader。有关更多信息,请参阅https://flask-login.readthedocs.io/en/latest/#how-it-works。

在定义user_loader类(随处可见)之后,我立即写了一个User

@login.user_loader
def load_user(id):
    return User.query.get(int(id))

该页面的每次刷新都会持续存在该错误,直到我重置烧瓶应用程序本身为止,尽管它处于调试模式。然后错误消失了。

这是已知错误还是可以预期的?

1 个答案:

答案 0 :(得分:0)

我最近遇到了这个问题。我也收到此错误:

Exception: No user_loader has been installed for this LoginManager. Refer to https://flask-login.readthedocs.io/en/latest/#how-it-works for more info.

这是我对user_loader函数的声明:

    @login_manager.user_loader
    def load_user(user_id):
        # since the user_id is just the primary key of our user table, use it in the query for the user
        return User.query.get(int(user_id))

事实证明,当我从本教程复制粘贴时,我粘贴了load_user函数 AFTER

return app

换句话说,代码的执行从未达到我的user_loader声明。确保ret​​urn语句位于user_loader声明之后。

相关问题