class StatsMiddleware(object):
    duration = 0

def process_request(self, request):
    request.start_time = time.time()

def process_response(self, request, response):
    self.duration = time.time() - request.start_time
response['x-server-time'] = datetime.now().strftime('%d/%m/%Y %H:%M')
response.content = response.content.replace('server_time_777', str.encode(
    response['x-server-time']))
response['x-page-generation-duration-ms'] = '{:.3f}'.format(self.duration)
response.content = response.content.replace('gen_duration_time_777', str.
    encode(response['x-page-generation-duration-ms']))
return response
Django Middleware - How to edit the HTML of a Django Response object?