diff --git a/django/views/static.py b/django/views/static.py index 289d26ef55e..c9d4b0317a7 100644 --- a/django/views/static.py +++ b/django/views/static.py @@ -91,13 +91,16 @@ def directory_index(path, fullpath): ]) except TemplateDoesNotExist: t = Engine(libraries={'i18n': 'django.templatetags.i18n'}).from_string(DEFAULT_DIRECTORY_INDEX_TEMPLATE) + c = Context() + else: + c = {} files = [] for f in os.listdir(fullpath): if not f.startswith('.'): if os.path.isdir(os.path.join(fullpath, f)): f += '/' files.append(f) - c = Context({ + c.update({ 'directory': path + '/', 'file_list': files, }) diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt index 78715791e76..a76806c2fb1 100644 --- a/docs/releases/1.11.1.txt +++ b/docs/releases/1.11.1.txt @@ -55,3 +55,6 @@ Bugfixes * Fixed a regression causing incorrect queries for ``__in`` subquery lookups when models use ``ForeignKey.to_field`` (:ticket:`28101`). + +* Fixed crash when overriding the template of + ``django.views.static.directory_index()`` (:ticket:`28122`). diff --git a/tests/view_tests/tests/test_static.py b/tests/view_tests/tests/test_static.py index 0d3b599767a..ab376ba386b 100644 --- a/tests/view_tests/tests/test_static.py +++ b/tests/view_tests/tests/test_static.py @@ -112,6 +112,20 @@ class StaticTests(SimpleTestCase): response = self.client.get('/%s/' % self.prefix) self.assertContains(response, 'Index of ./') + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'OPTIONS': { + 'loaders': [ + ('django.template.loaders.locmem.Loader', { + 'static/directory_index.html': 'Test index', + }), + ], + }, + }]) + def test_index_custom_template(self): + response = self.client.get('/%s/' % self.prefix) + self.assertEqual(response.content, b'Test index') + class StaticHelperTest(StaticTests): """