diff --git a/django/views/debug.py b/django/views/debug.py index bd0a6ba0ba..b555610d4a 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -245,6 +245,9 @@ class SafeExceptionReporterFilter: class ExceptionReporter: """Organize and coordinate reporting on exceptions.""" + html_template_path = CURRENT_DIR / 'templates' / 'technical_500.html' + text_template_path = CURRENT_DIR / 'templates' / 'technical_500.txt' + def __init__(self, request, exc_type, exc_value, tb, is_email=False): self.request = request self.filter = get_exception_reporter_filter(self.request) @@ -331,14 +334,14 @@ class ExceptionReporter: def get_traceback_html(self): """Return HTML version of debug 500 HTTP error page.""" - with Path(CURRENT_DIR, 'templates', 'technical_500.html').open(encoding='utf-8') as fh: + with self.html_template_path.open(encoding='utf-8') as fh: t = DEBUG_ENGINE.from_string(fh.read()) c = Context(self.get_traceback_data(), use_l10n=False) return t.render(c) def get_traceback_text(self): """Return plain text version of debug 500 HTTP error page.""" - with Path(CURRENT_DIR, 'templates', 'technical_500.txt').open(encoding='utf-8') as fh: + with self.text_template_path.open(encoding='utf-8') as fh: t = DEBUG_ENGINE.from_string(fh.read()) c = Context(self.get_traceback_data(), autoescape=False, use_l10n=False) return t.render(c) diff --git a/docs/howto/error-reporting.txt b/docs/howto/error-reporting.txt index c77f1e955e..e2dbe7ca27 100644 --- a/docs/howto/error-reporting.txt +++ b/docs/howto/error-reporting.txt @@ -325,6 +325,22 @@ Your custom reporter class needs to inherit from .. class:: ExceptionReporter + .. attribute:: html_template_path + + .. versionadded:: 3.2 + + A :class:`pathlib.Path` representing the absolute filesystem path to a + template for rendering the HTML representation of the exception. + Defaults to the Django provided template. + + .. attribute:: text_template_path + + .. versionadded:: 3.2 + + A :class:`pathlib.Path` representing the absolute filesystem path to a + template for rendering the plain-text representation of the exception. + Defaults to the Django provided template. + .. method:: get_traceback_data() Return a dictionary containing traceback information. diff --git a/docs/releases/3.2.txt b/docs/releases/3.2.txt index 0c233f796b..d61f66733d 100644 --- a/docs/releases/3.2.txt +++ b/docs/releases/3.2.txt @@ -188,7 +188,10 @@ Email Error Reporting ~~~~~~~~~~~~~~~ -* ... +* Custom :class:`~django.views.debug.ExceptionReporter` subclasses can now set + the :attr:`~django.views.debug.ExceptionReporter.html_template_path` and + :attr:`~django.views.debug.ExceptionReporter.text_template_path` class + attributes to override the templates used to render exception reports. File Storage ~~~~~~~~~~~~ diff --git a/tests/view_tests/templates/my_technical_500.html b/tests/view_tests/templates/my_technical_500.html new file mode 100644 index 0000000000..7e8f7a900d --- /dev/null +++ b/tests/view_tests/templates/my_technical_500.html @@ -0,0 +1 @@ +