我使用this示例创建自定义中间件,但我稍微改了一下。调试时,我看到代码进入该类,并进入函数定义,但它没有进入函数内部。我使用的是django 1.10。
这是我的中间件类
class GlobalThreadVarMiddleware(object):
_threadmap = {}
@classmethod
def get_current_data_manager(cls):
return cls._threadmap[_thread.get_ident()]['data_manager']
def process_request(self, request):
self._threadmap[_thread.get_ident()] = {}
self._threadmap[_thread.get_ident()]['data_manager'] = DataManager()
def process_exception(self, request, exception):
try:
del self._threadmap[_thread.get_ident()]
except KeyError:
pass
def process_response(self, request, response):
try:
del self._threadmap[_thread.get_ident()]['data_manager']
except KeyError:
pass
return response
添加到MIDDLEWARE:
MIDDLEWARE = [
'workflows.api.global_thread_variables_middleware.GlobalThreadVarMiddleware'
....
]