diff --git a/django/template/autoreload.py b/django/template/autoreload.py index f3fd704031..01dd564da4 100644 --- a/django/template/autoreload.py +++ b/django/template/autoreload.py @@ -31,12 +31,19 @@ def get_template_directories(): def reset_loaders(): + from django.forms.renderers import get_default_renderer + for backend in engines.all(): if not isinstance(backend, DjangoTemplates): continue for loader in backend.engine.template_loaders: loader.reset() + backend = get_default_renderer().engine + if isinstance(backend, DjangoTemplates): + for loader in backend.engine.template_loaders: + loader.reset() + @receiver(autoreload_started, dispatch_uid="template_loaders_watch_changes") def watch_for_template_changes(sender, **kwargs): diff --git a/tests/template_tests/test_autoreloader.py b/tests/template_tests/test_autoreloader.py index b8c16961bf..4fd7549a70 100644 --- a/tests/template_tests/test_autoreloader.py +++ b/tests/template_tests/test_autoreloader.py @@ -56,6 +56,23 @@ class TemplateReloadTests(SimpleTestCase): self.assertIsNone(autoreload.template_changed(None, Path(__file__))) mock_reset.assert_not_called() + @mock.patch("django.forms.renderers.get_default_renderer") + def test_form_template_reset_template_change(self, mock_renderer): + template_path = Path(__file__).parent / "templates" / "index.html" + self.assertIs(autoreload.template_changed(None, template_path), True) + mock_renderer.assert_called_once() + + @mock.patch("django.template.loaders.cached.Loader.reset") + def test_form_template_reset_template_change_reset_call(self, mock_loader_reset): + template_path = Path(__file__).parent / "templates" / "index.html" + self.assertIs(autoreload.template_changed(None, template_path), True) + mock_loader_reset.assert_called_once() + + @mock.patch("django.forms.renderers.get_default_renderer") + def test_form_template_reset_non_template_change(self, mock_renderer): + self.assertIsNone(autoreload.template_changed(None, Path(__file__))) + mock_renderer.assert_not_called() + def test_watch_for_template_changes(self): mock_reloader = mock.MagicMock() autoreload.watch_for_template_changes(mock_reloader)