diff --git a/django/contrib/staticfiles/handlers.py b/django/contrib/staticfiles/handlers.py index 2ec6d07d71..0c85c1ce40 100644 --- a/django/contrib/staticfiles/handlers.py +++ b/django/contrib/staticfiles/handlers.py @@ -4,6 +4,7 @@ from urllib.request import url2pathname from django.conf import settings from django.contrib.staticfiles import utils from django.contrib.staticfiles.views import serve +from django.core.handlers.exception import response_for_exception from django.core.handlers.wsgi import WSGIHandler, get_path_info @@ -59,6 +60,7 @@ class StaticFilesHandler(WSGIHandler): if settings.DEBUG: from django.views import debug return debug.technical_404_response(request, e) + return response_for_exception(request, e) return super().get_response(request) def __call__(self, environ, start_response): diff --git a/tests/staticfiles_tests/test_management.py b/tests/staticfiles_tests/test_management.py index 1472c7a488..74c7ef9fc6 100644 --- a/tests/staticfiles_tests/test_management.py +++ b/tests/staticfiles_tests/test_management.py @@ -16,7 +16,7 @@ from django.contrib.staticfiles.management.commands import ( ) from django.core.exceptions import ImproperlyConfigured from django.core.management import CommandError, call_command -from django.test import override_settings +from django.test import RequestFactory, override_settings from django.test.utils import extend_sys_path from django.utils import timezone from django.utils._os import symlinks_supported @@ -44,6 +44,18 @@ class TestRunserver(StaticFilesTestCase): command.get_handler(use_static_handler=True, insecure_serving=True) self.assertEqual(mocked.call_count, 1) + def test_404_response(self): + command = runserver.Command() + handler = command.get_handler(use_static_handler=True, insecure_serving=True) + missing_static_file = os.path.join(settings.STATIC_URL, 'unknown.css') + req = RequestFactory().get(missing_static_file) + with override_settings(DEBUG=False): + response = handler.get_response(req) + self.assertEqual(response.status_code, 404) + with override_settings(DEBUG=True): + response = handler.get_response(req) + self.assertEqual(response.status_code, 404) + class TestFindStatic(TestDefaults, CollectionTestCase): """