From 6815da6e9497a9a630d9f3637a51134d868bcd5b Mon Sep 17 00:00:00 2001 From: Hrushikesh Vaidya Date: Thu, 6 Jan 2022 19:52:57 +0530 Subject: [PATCH] Fixed #33396 -- Added view name to technical 500 debug page. --- django/views/debug.py | 1 + django/views/templates/technical_500.html | 6 ++++ django/views/templates/technical_500.txt | 1 + tests/view_tests/tests/test_debug.py | 34 +++++++++++++++++++++++ tests/view_tests/urls.py | 1 + tests/view_tests/views.py | 8 ++++++ 6 files changed, 51 insertions(+) diff --git a/django/views/debug.py b/django/views/debug.py index 624cc3e35f..34bd57b33d 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -359,6 +359,7 @@ class ExceptionReporter: c['request_FILES_items'] = self.request.FILES.items() c['request_COOKIES_items'] = self.request.COOKIES.items() c['request_insecure_uri'] = self._get_raw_insecure_uri() + c['raising_view_name'] = get_caller(self.request) # Check whether exception info is available if self.exc_type: diff --git a/django/views/templates/technical_500.html b/django/views/templates/technical_500.html index a0f65eb162..c09f09a19b 100644 --- a/django/views/templates/technical_500.html +++ b/django/views/templates/technical_500.html @@ -132,6 +132,12 @@ Exception Location: {{ lastframe.filename }}, line {{ lastframe.lineno }}, in {{ lastframe.function }} +{% endif %} +{% if raising_view_name %} + + Raised during: + {{ raising_view_name }} + {% endif %} Python Executable: diff --git a/django/views/templates/technical_500.txt b/django/views/templates/technical_500.txt index 5c86a3139f..0747de5c85 100644 --- a/django/views/templates/technical_500.txt +++ b/django/views/templates/technical_500.txt @@ -35,6 +35,7 @@ Traceback (most recent call last): {% endfor %} {% if exception_type %}Exception Type: {{ exception_type }}{% if request %} at {{ request.path_info }}{% endif %} {% if exception_value %}Exception Value: {{ exception_value }}{% endif %}{% endif %}{% endif %} +{% if raising_view_name %}Raised during: {{ raising_view_name }}{% endif %} {% if request %}Request information: {% if user_str %}USER: {{ user_str }}{% endif %} diff --git a/tests/view_tests/tests/test_debug.py b/tests/view_tests/tests/test_debug.py index 7c49fa51f3..591013be36 100644 --- a/tests/view_tests/tests/test_debug.py +++ b/tests/view_tests/tests/test_debug.py @@ -199,6 +199,40 @@ class DebugViewTests(SimpleTestCase): html=True, ) + def test_technical_500(self): + with self.assertLogs('django.request', 'ERROR'): + response = self.client.get('/raises500/') + self.assertContains( + response, + 'Raised during:view_tests.views.raises500', + status_code=500, + html=True, + ) + with self.assertLogs('django.request', 'ERROR'): + response = self.client.get('/raises500/', HTTP_ACCEPT='text/plain') + self.assertContains( + response, + 'Raised during: view_tests.views.raises500', + status_code=500, + ) + + def test_classbased_technical_500(self): + with self.assertLogs('django.request', 'ERROR'): + response = self.client.get('/classbased500/') + self.assertContains( + response, + 'Raised during:view_tests.views.Raises500View', + status_code=500, + html=True, + ) + with self.assertLogs('django.request', 'ERROR'): + response = self.client.get('/classbased500/', HTTP_ACCEPT='text/plain') + self.assertContains( + response, + 'Raised during: view_tests.views.Raises500View', + status_code=500, + ) + def test_non_l10ned_numeric_ids(self): """ Numeric IDs and fancy traceback context blocks line numbers shouldn't be localized. diff --git a/tests/view_tests/urls.py b/tests/view_tests/urls.py index f394934f4d..7a20ff40fa 100644 --- a/tests/view_tests/urls.py +++ b/tests/view_tests/urls.py @@ -31,6 +31,7 @@ urlpatterns = [ path('technical404/', views.technical404, name='my404'), path('classbased404/', views.Http404View.as_view()), + path('classbased500/', views.Raises500View.as_view()), # i18n views path('i18n/', include('django.conf.urls.i18n')), diff --git a/tests/view_tests/views.py b/tests/view_tests/views.py index ed84286173..97695ef493 100644 --- a/tests/view_tests/views.py +++ b/tests/view_tests/views.py @@ -51,6 +51,14 @@ def raises500(request): return technical_500_response(request, *sys.exc_info()) +class Raises500View(View): + def get(self, request): + try: + raise Exception + except Exception: + return technical_500_response(request, *sys.exc_info()) + + def raises400(request): raise SuspiciousOperation