首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flask - session.logged_in未更新

Flask - session.logged_in未更新
EN

Stack Overflow用户
提问于 2014-04-04 03:48:51
回答 1查看 1.6K关注 0票数 0

我正在写一个简单的Flask应用程序,第一页将要求用户名/密码,并检查有效性,然后更新session.logged_in为真。我使用打印输出进行检查,session.logged_in实际上设置为"True“。登录后,将有一个html页面,其中包含指向其他一些链接的导航,例如:表格列表( html文件中的列表具有链接{{ url_for('table_list') }}。但是,当我使用foreman并单击该链接时,session.logged_in为None (不是True或False)

我的python代码是:

代码语言:javascript
复制
from flask import Flask, stream_with_context, request, Response, url_for, render_template, flash, session
...
app = Flask(__name__)
app.debug = True                # Enable debug-mode
app.secret_key = 'nosecret'

def stream_template(template_name, **context):
    app.update_template_context(context)
    t = app.jinja_env.get_template(template_name)
    rv = t.stream(context)
    # uncomment if you don't need immediate reaction
    ##rv.enable_buffering(5)
    return rv

@app.route('/')
def index():
    return render_template('signin.html')

@app.route('/', methods=['POST'])
def login():
    # Read from the from input
    usrn = request.form['username']
    pswd = request.form['password']

    if validate_login(usrn, pswd):
        session['logged_in'] = True
        print 'login1:', session.logged_in
        sys.stdout.flush()
        return Response(stream_with_context(stream_template('index.html', data=genData())))
    else:
        return Response(stream_template('fail.html', code=pswd, username=usrn))

.....
@app.route('/listtable')
def list_table():
    print 'login2:', session.logged_in
    sys.stdout.flush()
    return Response(stream_with_context(stream_template('listtables.html', loginsession=session.logged_in)))

控制台中的结果是:

代码语言:javascript
复制
login1: True
login2: none

我认为问题是因为我使用上下文进行流式传输,以某种方式中断了我目前拥有的会话。有谁有办法解决这个问题吗?非常感谢。

解决方法:不确定我的问题是什么,但是我创建了一个名为MySession的新类,并且在我的会话中有一个布尔值来保存logged_in状态。通过使用MySession类,我可以更新我当前拥有的当前会话和信息,我不确定为什么Flask会话对我无效

EN

回答 1

Stack Overflow用户

发布于 2014-04-04 04:13:07

here中,为什么不使用装饰器@login_required,或者如果需要在函数中使用它,则使用方法is_authenticated()。我不认为你应该你应该自己访问数据结构。即使要登录,我猜也有一个方法必须调用,而不是在会话中设置True。(您应该使用login_user()方法)。您可能想要通过this

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22847547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档