Ticket #15502: 15502-test-and-fix.diff

File 15502-test-and-fix.diff, 3.1 KB (added by Sam Thompson, 14 years ago)
  • django/template/loader.py

     
    191191
    192192def select_template(template_name_list):
    193193    "Given a list of template names, returns the first that can be loaded."
     194    not_found = []
    194195    for template_name in template_name_list:
    195196        try:
    196197            return get_template(template_name)
    197         except TemplateDoesNotExist:
     198        except TemplateDoesNotExist, e:
     199            if e.args[0] not in not_found:
     200                not_found.append(e.args[0])
    198201            continue
    199202    # If we get here, none of the templates could be loaded
    200     raise TemplateDoesNotExist(', '.join(template_name_list))
     203    raise TemplateDoesNotExist(', '.join(not_found))
    201204
    202205add_to_builtins('django.template.loader_tags')
  • tests/regressiontests/templates/tests.py

     
    237237            loader.template_source_loaders = old_loaders
    238238            settings.TEMPLATE_DEBUG = old_td
    239239
     240
     241    def test_include_missing_template(self):
     242        """
     243        Tests that the correct template is identified as not existing
     244        when {% include %} specifies a template that does not exist.
     245        """
     246
     247        # TEMPLATE_DEBUG must be true, otherwise the exception raised
     248        # during {% include %} processing will be suppressed.
     249        old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
     250        old_loaders = loader.template_source_loaders
     251
     252        try:
     253            # Test the base loader class via the app loader. load_template
     254            # from base is used by all shipped loaders excepting cached,
     255            # which has its own test.
     256            loader.template_source_loaders = (app_directories.Loader(),)
     257
     258            load_name = 'test_include_error.html'
     259            r = None
     260            try:
     261                tmpl = loader.select_template([load_name])
     262                r = tmpl.render(template.Context({}))
     263            except template.TemplateDoesNotExist, e:
     264                settings.TEMPLATE_DEBUG = old_td
     265                self.assertEqual(e.args[0], 'missing.html')
     266            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
     267        finally:
     268            loader.template_source_loaders = old_loaders
     269            settings.TEMPLATE_DEBUG = old_td
     270
     271
    240272    def test_extends_include_missing_baseloader(self):
    241273        """
    242274        Tests that the correct template is identified as not existing
  • tests/regressiontests/templates/templates/test_include_error.html

     
     1{% include "missing.html" %}
     2 No newline at end of file
Back to Top