有没有办法在中间件中使用flask会话? (烧瓶1.1.1)
我需要访问会话以获取应用程序请求前的“ user_id”。
from flask import session
class Middleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
1)
print(session['user_id']) # RuntimeError: Working outside of request context.
2)
with self.app.app_context(): # AttributeError: 'function' object has no attribute 'app_context'
print(session['user_id'])
3)
with self.app(environ, start_response).app_context():
# AttributeError: 'ClosingIterator' object has no attribute 'app_context'
谢谢。