flasklogin;未出现错误,但用户未登录

2024-03-28 19:22:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在回收一个旧应用程序。我发出了一个manager命令,用几个用户初始化数据库。然后,我尝试登录,使用以下方法:

@user.route('/login', methods=['GET', 'POST'])
def login():
    """ log the user in """
    form = LoginForm()
    if form.validate_on_submit():
        u = User.query.filter_by(email=form.email.data.lower()).first()
        if u is not None and u.verify_password(form.password.data):
            login_user(u)
            flash('successfully logged in', 'success')
            return redirect(url_for('root.home'))
        flash('invalid login')
    return render_template('user/login.html', form=form)

消息闪现,用户已成功登录,未引发任何错误。但是,没有任何页面更改以反映存在经过身份验证的用户。你知道吗

在我的根蓝图上(所有东西都以/为前缀,没有其他东西),我添加了以下内容来帮助调试

@root.before_app_request
def check_user():
    print('    is the user authenticated? {}\n    Who is the user? {}'.format(
        current_user.is_authenticated(),
        current_user
    ))

我提出了一些要求

 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
    is the user authenticated? False
    Who is the user? <flask_login.AnonymousUserMixin object at 0x7f2454642550>
127.0.0.1 - - [04/Jul/2014 12:57:57] "GET / HTTP/1.1" 200 -
    is the user authenticated? False
    Who is the user? <flask_login.AnonymousUserMixin object at 0x7f245460eda0>
127.0.0.1 - - [04/Jul/2014 12:58:05] "GET /user/login HTTP/1.1" 200 -
    is the user authenticated? False
    Who is the user? <flask_login.AnonymousUserMixin object at 0x7f245460eda0>
127.0.0.1 - - [04/Jul/2014 12:58:10] "POST /user/login HTTP/1.1" 302 -
    is the user authenticated? False
    Who is the user? <flask_login.AnonymousUserMixin object at 0x7f24545cedd8>

在这段没有让用户登录的代码中,会出现什么问题? 完整的源代码:https://github.com/DarkCrowz/innovation_center


Tags: the用户formfalseflaskgetobjectis
1条回答
网友
1楼 · 发布于 2024-03-28 19:22:27

您正在user loader中使用id()函数:

@login_manager.user_loader
def load_user(identification):
    return User.query.get(id(identification))

identification不是可重用的内存位置,但是您的函数将其视为是。也许你想在这里用int()?你知道吗

id()替换int(),我得到一个有效的会话并登录。你知道吗

相关问题 更多 >