Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #15502 -- Ensure that nested TemplateDoesNotExist errors are pr…

…opegated with a meaningful error message when loaded using select_template. Thanks to jaylett for the report, and GDorn for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15717 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit edfed185811821a6d000f32d22fe398dfe3e0fe9 1 parent 17bd349
@freakboy3742 freakboy3742 authored
View
7 django/template/loader.py
@@ -191,12 +191,15 @@ def render_to_string(template_name, dictionary=None, context_instance=None):
def select_template(template_name_list):
"Given a list of template names, returns the first that can be loaded."
+ not_found = []
for template_name in template_name_list:
try:
return get_template(template_name)
- except TemplateDoesNotExist:
+ except TemplateDoesNotExist, e:
+ if e.args[0] not in not_found:
+ not_found.append(e.args[0])
continue
# If we get here, none of the templates could be loaded
- raise TemplateDoesNotExist(', '.join(template_name_list))
+ raise TemplateDoesNotExist(', '.join(not_found))
add_to_builtins('django.template.loader_tags')
View
1  tests/regressiontests/templates/templates/test_include_error.html
@@ -0,0 +1 @@
+{% include "missing.html" %}
View
32 tests/regressiontests/templates/tests.py
@@ -237,6 +237,38 @@ def test_loader_debug_origin(self):
loader.template_source_loaders = old_loaders
settings.TEMPLATE_DEBUG = old_td
+
+ def test_include_missing_template(self):
+ """
+ Tests that the correct template is identified as not existing
+ when {% include %} specifies a template that does not exist.
+ """
+
+ # TEMPLATE_DEBUG must be true, otherwise the exception raised
+ # during {% include %} processing will be suppressed.
+ old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
+ old_loaders = loader.template_source_loaders
+
+ try:
+ # Test the base loader class via the app loader. load_template
+ # from base is used by all shipped loaders excepting cached,
+ # which has its own test.
+ loader.template_source_loaders = (app_directories.Loader(),)
+
+ load_name = 'test_include_error.html'
+ r = None
+ try:
+ tmpl = loader.select_template([load_name])
+ r = tmpl.render(template.Context({}))
+ except template.TemplateDoesNotExist, e:
+ settings.TEMPLATE_DEBUG = old_td
+ self.assertEqual(e.args[0], 'missing.html')
+ self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
+ finally:
+ loader.template_source_loaders = old_loaders
+ settings.TEMPLATE_DEBUG = old_td
+
+
def test_extends_include_missing_baseloader(self):
"""
Tests that the correct template is identified as not existing
Please sign in to comment.
Something went wrong with that request. Please try again.