Skip to content

Commit b0d07b1

Browse files
committed
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 `</body>` element (the INSERT_BEFORE pattern) and so will also not be included in the response. Fixes #1368
1 parent 9119982 commit b0d07b1

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

debug_toolbar/middleware.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,18 @@ def __call__(self, request):
6969

7070
response = self.generate_server_timing_header(response, toolbar.enabled_panels)
7171

72+
# Always render the toolbar for the history panel, even if it is not
73+
# included in the response.
74+
rendered = toolbar.render_toolbar()
75+
7276
# Check for responses where the toolbar can't be inserted.
7377
content_encoding = response.get("Content-Encoding", "")
7478
content_type = response.get("Content-Type", "").split(";")[0]
75-
if any(
76-
(
77-
getattr(response, "streaming", False),
78-
"gzip" in content_encoding,
79-
content_type not in _HTML_TYPES,
80-
request.is_ajax(),
81-
)
79+
if (
80+
getattr(response, "streaming", False)
81+
or "gzip" in content_encoding
82+
or content_type not in _HTML_TYPES
8283
):
83-
# If a AJAX or JSON request, render the toolbar for the history.
84-
if request.is_ajax() or content_type == "application/json":
85-
toolbar.render_toolbar()
8684
return response
8785

8886
# Insert the toolbar in the response.
@@ -91,7 +89,7 @@ def __call__(self, request):
9189
pattern = re.escape(insert_before)
9290
bits = re.split(pattern, content, flags=re.IGNORECASE)
9391
if len(bits) > 1:
94-
bits[-2] += toolbar.render_toolbar()
92+
bits[-2] += rendered
9593
response.content = insert_before.join(bits)
9694
if "Content-Length" in response:
9795
response["Content-Length"] = len(response.content)

0 commit comments

Comments
 (0)