summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsJn-227 <ishjain2712@gmail.com>2025-06-13 15:52:46 +0530
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-07-18 10:18:22 +0200
commitbe8c9b19baeb4daf6bf2e603673714c7536b657c (patch)
treed9a31d68125c9f1e7268fd00a16485eb632b1610
parent449b9f9aeeaa3a1529d2c29a9a43e87350177559 (diff)
Fixed #36456 -- Improved content type negotiation in technical 500 error response.
-rw-r--r--django/views/debug.py5
-rw-r--r--tests/view_tests/tests/test_debug.py16
2 files changed, 19 insertions, 2 deletions
diff --git a/django/views/debug.py b/django/views/debug.py
index 948cdcbf2f..9d8d1987de 100644
--- a/django/views/debug.py
+++ b/django/views/debug.py
@@ -65,9 +65,10 @@ def technical_500_response(request, exc_type, exc_value, tb, status_code=500):
the values returned from sys.exc_info() and friends.
"""
reporter = get_exception_reporter_class(request)(request, exc_type, exc_value, tb)
- if request.accepts("text/html"):
+ preferred_type = request.get_preferred_type(["text/html", "text/plain"])
+ if preferred_type == "text/html":
html = reporter.get_traceback_html()
- return HttpResponse(html, status=status_code)
+ return HttpResponse(html, status=status_code, content_type="text/html")
else:
text = reporter.get_traceback_text()
return HttpResponse(
diff --git a/tests/view_tests/tests/test_debug.py b/tests/view_tests/tests/test_debug.py
index a5a1d85e83..f5cb82cd2d 100644
--- a/tests/view_tests/tests/test_debug.py
+++ b/tests/view_tests/tests/test_debug.py
@@ -261,6 +261,22 @@ class DebugViewTests(SimpleTestCase):
status_code=500,
)
+ def test_technical_500_content_type_negotiation(self):
+ for accepts, content_type in [
+ ("text/plain", "text/plain; charset=utf-8"),
+ ("text/html", "text/html"),
+ ("text/html,text/plain;q=0.9", "text/html"),
+ ("text/plain,text/html;q=0.9", "text/plain; charset=utf-8"),
+ ("text/*", "text/html"),
+ ]:
+ with self.subTest(accepts=accepts):
+ with self.assertLogs("django.request", "ERROR"):
+ response = self.client.get(
+ "/raises500/", headers={"accept": accepts}
+ )
+ self.assertEqual(response.status_code, 500)
+ self.assertEqual(response["Content-Type"], content_type)
+
def test_classbased_technical_500(self):
with self.assertLogs("django.request", "ERROR"):
response = self.client.get("/classbased500/")