根据Flask文档,如果会话是新的,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
默认会话不会尝试跟踪会话是否为新会话。