From 229488c8a1486f25b1f2b5ca422cac67708c6c9d Mon Sep 17 00:00:00 2001 From: Ahmed Mohamed Date: Thu, 21 Jan 2016 17:40:33 +0900 Subject: [PATCH] Fixed #26109 -- Raised a helpful error if loader.select_tamplate() is passed a string. --- django/template/loader.py | 7 +++++++ tests/template_loader/tests.py | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/django/template/loader.py b/django/template/loader.py index 98e56817bf..44d5332b92 100644 --- a/django/template/loader.py +++ b/django/template/loader.py @@ -33,6 +33,13 @@ def select_template(template_name_list, using=None): Raises TemplateDoesNotExist if no such template exists. """ + if isinstance(template_name_list, six.string_types): + raise TypeError( + 'select_template() takes an iterable of template names but got a ' + 'string: %r. Use get_template() if you want to load a single ' + 'template by name.' % template_name_list + ) + chain = [] engines = _engine_list(using) for template_name in template_name_list: diff --git a/tests/template_loader/tests.py b/tests/template_loader/tests.py index ba712e521b..5e62574666 100644 --- a/tests/template_loader/tests.py +++ b/tests/template_loader/tests.py @@ -60,6 +60,15 @@ class TemplateLoaderTests(SimpleTestCase): with self.assertRaises(TemplateDoesNotExist): select_template([]) + def test_select_template_string(self): + with self.assertRaisesMessage( + TypeError, + "select_template() takes an iterable of template names but got a " + "string: 'template_loader/hello.html'. Use get_template() if you " + "want to load a single template by name." + ): + select_template('template_loader/hello.html') + def test_select_template_not_found(self): with self.assertRaises(TemplateDoesNotExist) as e: select_template(["template_loader/unknown.html",