Skip to main content
 首页 » 编程设计

python之Flask 中 Session 类的新属性的意外工作

2025年01月19日12zhoujg

根据Flask文档中,如果 session 是新的,new 属性返回 True。否则 False,但在我的例子中,无论如何它总是返回 False。这是代码:

@app.route('/set-session/') 
def testing_session(): 
    session['user_id'] = 'jon' 
    print(session.new)    # always returns True . Why ? 
    print(session.modified) 
    return 'playing with session' 

请您参考如下方法:

来自code :

#: some session backends can tell you if a session is new, but that is 
#: not necessarily guaranteed.  Use with caution.  The default mixin 
#: implementation just hardcodes ``False`` in. 
new = False 

默认 session 不会尝试跟踪 session 是否是新的。

参见 this issue on GitHub