from threading import local class RequestLocals local pass request_loc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from threading import local
class RequestLocals(local):
pass
request_locals = RequestLocals()
class RequestLocalsMiddleware(object):
def process_request(self, request):
request_locals.request = request
def process_response(self, request, response):
request_locals.request = None
return response
def process_exception(self, request, exception):
request_locals.request = None