from threading import local from django.utils.deprecation import MiddlewareMixin _user = local() _request = local() class CurrentUserMiddleware(MiddlewareMixin): def process_request(self, request): _user.value = request.user _request.value = request def get_current_user(): try: return _user.value except AttributeError: return None def get_current_request(): try: return _request.value except AttributeError: return None