Skip to content

Enhance RedirectsPanel with customizable redirect response hook #2104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions debug_toolbar/panels/redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,8 @@ def _process_response(self, response):
Common response processing logic.
"""
if 300 <= response.status_code < 400:
redirect_to = response.get("Location")
if redirect_to:
status_line = f"{response.status_code} {response.reason_phrase}"
cookies = response.cookies
context = {
"redirect_to": redirect_to,
"status_line": status_line,
"toolbar": self.toolbar,
}
# Using SimpleTemplateResponse avoids running global context processors.
response = SimpleTemplateResponse(
"debug_toolbar/redirect.html", context
)
response.cookies = cookies
if redirect_to := response.get("Location"):
response = self.get_interception_response(response, redirect_to)
response.render()
return response

Expand All @@ -53,3 +41,22 @@ def process_request(self, request):
if iscoroutine(response):
return self.aprocess_request(request, response)
return self._process_response(response)

def get_interception_response(self, response, redirect_to):
"""
Hook method to allow subclasses to customize the interception response.
"""
status_line = f"{response.status_code} {response.reason_phrase}"
cookies = response.cookies
original_response = response
context = {
"redirect_to": redirect_to,
"status_line": status_line,
"toolbar": self.toolbar,
"original_response": original_response,
}
# Using SimpleTemplateResponse avoids running global context processors.
response = SimpleTemplateResponse("debug_toolbar/redirect.html", context)
response.cookies = cookies
response.original_response = original_response
return response
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Change log
Pending
-------

* Added hook to RedirectsPanel for subclass customization.

5.1.0 (2025-03-20)
------------------
* Added Django 5.2 to the tox matrix.
Expand Down
5 changes: 5 additions & 0 deletions docs/panels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ Since this behavior is annoying when you aren't debugging a redirect, this
panel is included but inactive by default. You can activate it by default with
the ``DISABLE_PANELS`` configuration option.

To further customize the behavior, you can subclass the ``RedirectsPanel``
and override the ``get_interception_response`` method to manipulate the
response directly. To use a custom ``RedirectsPanel``, you need to replace
the original one in ``DEBUG_TOOLBAR_PANELS`` in your ``settings.py``.

.. _profiling-panel:

Profiling
Expand Down
13 changes: 13 additions & 0 deletions tests/panels/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,16 @@ async def get_response(request):
response = await self.panel.process_request(self.request)
self.assertIsInstance(response, HttpResponse)
self.assertTrue(response is await_response)

def test_original_response_preserved(self):
redirect = HttpResponse(status=302)
redirect["Location"] = "http://somewhere/else/"
self._get_response = lambda request: redirect
response = self.panel.process_request(self.request)
self.assertFalse(response is redirect)
self.assertTrue(hasattr(response, "original_response"))
self.assertTrue(response.original_response is redirect)
self.assertIsNone(response.get("Location"))
self.assertEqual(
response.original_response.get("Location"), "http://somewhere/else/"
)