class FixIP (object): """Middleware that sets the IP based on X-Forwarded-For. This is useful if you are sitting behind a reverse proxy.""" def process_request(self, request): try: realip = request.META['HTTP_X_FORWARDED_FOR'] realip = realip.split(",")[0] request.META['REMOTE_ADDR'] = realip except KeyError: return