From b0d07b1b7cbd36f2b9f596be20ab41f590e93f68 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 11 Oct 2020 09:45:48 -0700 Subject: [PATCH] Remove calls to deprecated request.is_ajax() Since Django 3.1, the method request.is_ajax() is deprecated and will be removed in a future version. Using Django 3.1 with django-debug-toolbar results in the warning: django-debug-toolbar/debug_toolbar/middleware.py:80: RemovedInDjango40Warning: request.is_ajax() is deprecated. See Django 3.1 release notes for more details about this deprecation. request.is_ajax(), The calls were removed with no replacements as these check are unnecessary anyway. If the response is not HTML (e.g. JSON) then the check `or content_type not in _HTML_TYPES` will evaluate as false and the toolbar will not be included in the response. If the response is an HTML fragment, it will not contain an `` element (the INSERT_BEFORE pattern) and so will also not be included in the response. Fixes #1368 --- debug_toolbar/middleware.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index 35eceee5c..69100b20d 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -69,20 +69,18 @@ def __call__(self, request): response = self.generate_server_timing_header(response, toolbar.enabled_panels) + # Always render the toolbar for the history panel, even if it is not + # included in the response. + rendered = toolbar.render_toolbar() + # Check for responses where the toolbar can't be inserted. content_encoding = response.get("Content-Encoding", "") content_type = response.get("Content-Type", "").split(";")[0] - if any( - ( - getattr(response, "streaming", False), - "gzip" in content_encoding, - content_type not in _HTML_TYPES, - request.is_ajax(), - ) + if ( + getattr(response, "streaming", False) + or "gzip" in content_encoding + or content_type not in _HTML_TYPES ): - # If a AJAX or JSON request, render the toolbar for the history. - if request.is_ajax() or content_type == "application/json": - toolbar.render_toolbar() return response # Insert the toolbar in the response. @@ -91,7 +89,7 @@ def __call__(self, request): pattern = re.escape(insert_before) bits = re.split(pattern, content, flags=re.IGNORECASE) if len(bits) > 1: - bits[-2] += toolbar.render_toolbar() + bits[-2] += rendered response.content = insert_before.join(bits) if "Content-Length" in response: response["Content-Length"] = len(response.content)