烧瓶的登录;没有引发错误,但用户未登录

时间:2014-07-04 17:00:52

标签: python python-3.x flask

我正在回收一个旧的应用程序。我做了一个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
    ))

wben我提出了一些请求,

 * 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

1 个答案:

答案 0 :(得分:2)

您正在使用user loader中的id()功能:

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

identification 是一个重复使用的内存位置,但您的功能会将其视为原样。也许你打算在这里使用int()

id()替换为int(),我获得了有效的会话并登录。