Ticket #12787: 12787.2.diff

File 12787.2.diff, 11.1 KB (added by Karen Tracey, 15 years ago)
  • django/template/loaders/cached.py

     
    3737        if template_name not in self.template_cache:
    3838            template, origin = self.find_template(template_name, template_dirs)
    3939            if not hasattr(template, 'render'):
    40                 template = get_template_from_string(template, origin, template_name)
     40                try:
     41                    template = get_template_from_string(template, origin, template_name)
     42                except TemplateDoesNotExist:
     43                    return template, origin
    4144            self.template_cache[template_name] = template
    4245        return self.template_cache[template_name], None
    4346
  • django/template/loader_tags.py

     
    9797            raise TemplateSyntaxError(error_msg)
    9898        if hasattr(parent, 'render'):
    9999            return parent # parent is a Template object
    100         try:
    101             return get_template(parent)
    102         except TemplateDoesNotExist:
    103             raise TemplateSyntaxError("Template %r cannot be extended, because it doesn't exist" % parent)
     100        return get_template(parent)
    104101
    105102    def render(self, context):
    106103        compiled_parent = self.get_parent(context)
  • django/template/loader.py

     
    4444    def load_template(self, template_name, template_dirs=None):
    4545        source, display_name = self.load_template_source(template_name, template_dirs)
    4646        origin = make_origin(display_name, self.load_template_source, template_name, template_dirs)
    47         template = get_template_from_string(source, origin, template_name)
    48         return template, None
     47        try:
     48            template = get_template_from_string(source, origin, template_name)
     49            return template, None
     50        except TemplateDoesNotExist:
     51            return source, display_name
    4952
    5053    def load_template_source(self, template_name, template_dirs=None):
    5154        """
  • tests/regressiontests/templates/tests.py

     
    160160        # Turn TEMPLATE_DEBUG on, so that the origin file name will be kept with
    161161        # the compiled templates.
    162162        old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
     163        old_loaders = loader.template_source_loaders
    163164
     165        try:
     166            loader.template_source_loaders = (filesystem.Loader(),)
     167
     168            # We rely on the fact that runtests.py sets up TEMPLATE_DIRS to
     169            # point to a directory containing a 404.html file. Also that
     170            # the file system and app directories loaders both inherit the
     171            # load_template method from the BaseLoader class, so we only need
     172            # to test one of them.
     173            load_name = '404.html'
     174            template = loader.get_template(load_name)
     175            template_name = template.nodelist[0].source[0].name
     176            self.assertTrue(template_name.endswith(load_name),
     177                'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name)
     178
     179            # Aso test the cached loader, since it overrides load_template
     180            cache_loader = cached.Loader(('',))
     181            cache_loader._cached_loaders = loader.template_source_loaders
     182            loader.template_source_loaders = (cache_loader,)
     183
     184            template = loader.get_template(load_name)
     185            template_name = template.nodelist[0].source[0].name
     186            self.assertTrue(template_name.endswith(load_name),
     187                'Template loaded through cached loader has incorrect name for debug page: %s' % template_name)
     188
     189            template = loader.get_template(load_name)
     190            template_name = template.nodelist[0].source[0].name
     191            self.assertTrue(template_name.endswith(load_name),
     192                'Cached template loaded through cached loader has incorrect name for debug page: %s' % template_name)
     193        finally:
     194            loader.template_source_loaders = old_loaders
     195            settings.TEMPLATE_DEBUG = old_td
     196
     197    def test_does_not_exist_name_app(self):
     198        old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
    164199        old_loaders = loader.template_source_loaders
    165         loader.template_source_loaders = (filesystem.Loader(),)
    166200
    167         # We rely on the fact that runtests.py sets up TEMPLATE_DIRS to
    168         # point to a directory containing a 404.html file. Also that
    169         # the file system and app directories loaders both inherit the
    170         # load_template method from the BaseLoader class, so we only need
    171         # to test one of them.
    172         load_name = '404.html'
    173         template = loader.get_template(load_name)
    174         template_name = template.nodelist[0].source[0].name
    175         self.assertTrue(template_name.endswith(load_name),
    176             'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name)
     201        try:
     202            loader.template_source_loaders = (app_directories.Loader(),)
    177203
    178         # Aso test the cached loader, since it overrides load_template
    179         cache_loader = cached.Loader(('',))
    180         cache_loader._cached_loaders = loader.template_source_loaders
    181         loader.template_source_loaders = (cache_loader,)
     204            load_name = 'test_extends_error.html'
     205            tmpl = loader.get_template(load_name)
     206            r = None
     207            try:
     208                r = tmpl.render(template.Context({}))
     209            except template.TemplateSyntaxError, e:
     210                settings.TEMPLATE_DEBUG = old_td
     211                self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
     212            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
     213        finally:
     214            loader.template_source_loaders = old_loaders
     215            settings.TEMPLATE_DEBUG = old_td
    182216
    183         template = loader.get_template(load_name)
    184         template_name = template.nodelist[0].source[0].name
    185         self.assertTrue(template_name.endswith(load_name),
    186             'Template loaded through cached loader has incorrect name for debug page: %s' % template_name)
     217    def test_does_not_exist_name_cache(self):
     218        old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
     219        old_loaders = loader.template_source_loaders
    187220
    188         template = loader.get_template(load_name)
    189         template_name = template.nodelist[0].source[0].name
    190         self.assertTrue(template_name.endswith(load_name),
    191             'Cached template loaded through cached loader has incorrect name for debug page: %s' % template_name)
     221        try:
     222            cache_loader = cached.Loader(('',))
     223            cache_loader._cached_loaders = (app_directories.Loader(),)
     224            loader.template_source_loaders = (cache_loader,)
    192225
    193         loader.template_source_loaders = old_loaders
    194         settings.TEMPLATE_DEBUG = old_td
     226            load_name = 'test_extends_error.html'
     227            tmpl = loader.get_template(load_name)
     228            r = None
     229            try:
     230                r = tmpl.render(template.Context({}))
     231            except template.TemplateSyntaxError, e:
     232                self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
     233            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
    195234
     235            # For the cached loader, repeat the test, to ensure the first attempt did not cache a
     236            # result that behaves incorrectly on subsequent attempts.
     237            tmpl = loader.get_template(load_name)
     238            try:
     239                tmpl.render(template.Context({}))
     240            except template.TemplateSyntaxError, e:
     241                self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
     242            self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
     243        finally:
     244            loader.template_source_loaders = old_loaders
     245            settings.TEMPLATE_DEBUG = old_td
     246
    196247    def test_token_smart_split(self):
    197248        # Regression test for #7027
    198249        token = template.Token(template.TOKEN_BLOCK, 'sometag _("Page not found") value|yesno:_("yes,no")')
     
    269320            if isinstance(vals[2], tuple):
    270321                normal_string_result = vals[2][0]
    271322                invalid_string_result = vals[2][1]
    272                 if '%s' in invalid_string_result:
     323                if isinstance(invalid_string_result, basestring) and '%s' in invalid_string_result:
    273324                    expected_invalid_str = 'INVALID %s'
    274325                    invalid_string_result = invalid_string_result % vals[2][2]
    275326                    template.invalid_var_format_string = True
     
    530581            ### EXCEPTIONS ############################################################
    531582
    532583            # Raise exception for invalid template name
    533             'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
     584            'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateDoesNotExist),
    534585
    535586            # Raise exception for invalid template name (in variable)
    536             'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
     587            'exception02': ("{% extends nonexistent %}", {}, (template.TemplateSyntaxError, template.TemplateDoesNotExist)),
    537588
    538589            # Raise exception for extra {% extends %} tags
    539590            'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),
  • tests/regressiontests/templates/templates/test_extends_error.html

     
     1{% extends "broken_base.html" %}
  • tests/regressiontests/templates/templates/broken_base.html

    Property changes on: tests/regressiontests/templates/templates/test_extends_error.html
    ___________________________________________________________________
    Added: svn:executable
       + *
    Added: svn:eol-style
       + native
    
     
     1{% include "missing.html" %}
Back to Top