Ticket #24577: 24577.diff

File 24577.diff, 3.9 KB (added by Tim Graham, 8 years ago)
  • django/template/loader_tags.py

    diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py
    index 7b4de9a..2655b88 100644
    a b from collections import defaultdict  
    66from django.utils.deprecation import RemovedInDjango21Warning
    77from django.utils.safestring import mark_safe
    88
     9from . import engines
    910from .base import (
    1011    Node, Template, TemplateSyntaxError, TextNode, Variable, token_kwargs,
    1112)
    BLOCK_CONTEXT_KEY = 'block_context'  
    1819logger = logging.getLogger('django.template')
    1920
    2021
     22class DifferentTemplateEngineError(Exception):
     23    pass
     24
     25
    2126class BlockContext:
    2227    def __init__(self):
    2328        # Dictionary of FIFO queues.
    class IncludeNode(Node):  
    183188            # Use the base.Template of a backends.django.Template.
    184189            elif hasattr(template, 'template'):
    185190                template = template.template
     191
     192            if context.template.engine != template.engine:
     193                raise DifferentTemplateEngineError(
     194                    'Included template %r is from engine %r but parent template '
     195                    '%r is from engine %r. Including a template from a '
     196                    'different engine is prohibited.' % (
     197                        template.name,
     198                        engines.get_name_from_engine(template.engine),
     199                        context.template.name,
     200                        engines.get_name_from_engine(context.template.engine),
     201                    )
     202                )
    186203            values = {
    187204                name: var.resolve(context)
    188205                for name, var in self.extra_context.items()
    class IncludeNode(Node):  
    191208                return template.render(context.new(values))
    192209            with context.push(**values):
    193210                return template.render(context)
     211        except DifferentTemplateEngineError:
     212            raise
    194213        except Exception as e:
    195214            if context.template.engine.debug:
    196215                raise
  • django/template/utils.py

    diff --git a/django/template/utils.py b/django/template/utils.py
    index fde2f64..fad3e90 100644
    a b class EngineHandler:  
    8787    def all(self):
    8888        return [self[alias] for alias in self]
    8989
     90    def get_name_from_engine(self, engine):
     91        try:
     92            return next(e.name for e in self.all() if e.engine is engine)
     93        except StopIteration:
     94            return 'unnamed'
     95
    9096
    9197@functools.lru_cache()
    9298def get_app_template_dirs(dirname):
  • tests/template_tests/syntax_tests/test_include.py

    diff --git a/tests/template_tests/syntax_tests/test_include.py b/tests/template_tests/syntax_tests/test_include.py
    index 2b08f3f..28b20e0 100644
    a b import warnings  
    33from django.template import (
    44    Context, Engine, TemplateDoesNotExist, TemplateSyntaxError, loader,
    55)
     6from django.template.loader_tags import DifferentTemplateEngineError
    67from django.test import SimpleTestCase, ignore_warnings
    78from django.utils.deprecation import RemovedInDjango21Warning
    89
    class IncludeTests(SimpleTestCase):  
    282283        output = tmpl.render({'tmpl': loader.get_template('index.html')})
    283284        self.assertEqual(output, 'index\n\n')
    284285
     286    def test_include_from_other_engine(self):
     287        engine = Engine()
     288        ctx = Context({'tmpl': loader.get_template('index.html')})
     289        outer_tmpl = engine.from_string('{% include tmpl %}')
     290        msg = (
     291            "Included template 'index.html' is from engine 'django' but parent "
     292            "template None is from engine 'unnamed'. Including a template from "
     293            "a different engine is prohibited."
     294        )
     295        with self.assertRaisesMessage(DifferentTemplateEngineError, msg):
     296            outer_tmpl.render(ctx)
     297
    285298    def test_include_immediate_missing(self):
    286299        """
    287300        #16417 -- Include tags pointing to missing templates should not raise
Back to Top