diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2015-02-05 11:53:04 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2015-02-05 20:09:22 +0100 |
| commit | aed1b1f6e5133f91c063f6f8b75729c4a18f99c7 (patch) | |
| tree | b741938589c82cc616d09673456b317beafdd606 | |
| parent | 67787db22a3d684ce2800c80fca477af1684deab (diff) | |
[1.8.x] Fixed #24265 -- Preserved template backend loading exceptions.
If importing or initializing a template backend fails, attempting to
access this template backend again must raise the same exception.
Backport of 44ad6915 from master
| -rw-r--r-- | django/template/utils.py | 4 | ||||
| -rw-r--r-- | tests/template_backends/test_utils.py | 37 |
2 files changed, 41 insertions, 0 deletions
diff --git a/django/template/utils.py b/django/template/utils.py index 3303c1ccb7..be2166810c 100644 --- a/django/template/utils.py +++ b/django/template/utils.py @@ -90,6 +90,10 @@ class EngineHandler(object): "Could not find config for '{}' " "in settings.TEMPLATES".format(alias)) + # If importing or initializing the backend raises an exception, + # self._engines[alias] isn't set and this code may get executed + # again, so we must preserve the original params. See #24265. + params = params.copy() backend = params.pop('BACKEND') engine_cls = import_string(backend) engine = engine_cls(params) diff --git a/tests/template_backends/test_utils.py b/tests/template_backends/test_utils.py new file mode 100644 index 0000000000..3d2de99015 --- /dev/null +++ b/tests/template_backends/test_utils.py @@ -0,0 +1,37 @@ +from django.core.exceptions import ImproperlyConfigured +from django.template import engines +from django.test import SimpleTestCase, override_settings + + +class TemplateStringsTests(SimpleTestCase): + + @override_settings(TEMPLATES=[{ + 'BACKEND': 'raise.import.error', + }]) + def test_backend_import_error(self): + """ + Failing to import a backend keeps raising the original import error. + + Regression test for #24265. + """ + with self.assertRaises(ImportError): + engines.all() + with self.assertRaises(ImportError): + engines.all() + + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + # Incorrect: APP_DIRS and loaders are mutually incompatible. + 'APP_DIRS': True, + 'OPTIONS': {'loaders': []}, + }]) + def test_backend_improperly_configured(self): + """ + Failing to initialize a backend keeps raising the original exception. + + Regression test for #24265. + """ + with self.assertRaises(ImproperlyConfigured): + engines.all() + with self.assertRaises(ImproperlyConfigured): + engines.all() |
