diff --git a/django/template/context.py b/django/template/context.py
index bcfaa3b..acc5758 100644
a
|
b
|
class ContextPopException(Exception):
|
14 | 14 | "pop() has been called more times than push()" |
15 | 15 | pass |
16 | 16 | |
17 | | class EmptyClass(object): |
18 | | # No-op class which takes no args to its __init__ method, to help implement |
19 | | # __copy__ |
20 | | pass |
21 | | |
22 | 17 | class BaseContext(object): |
23 | 18 | def __init__(self, dict_=None): |
24 | | dict_ = dict_ or {} |
25 | | self.dicts = [dict_] |
| 19 | self._reset_dicts(dict_) |
| 20 | |
| 21 | def _reset_dicts(self, value=None): |
| 22 | self.dicts = [value or {}] |
26 | 23 | |
27 | 24 | def __copy__(self): |
28 | | duplicate = EmptyClass() |
29 | | duplicate.__class__ = self.__class__ |
30 | | duplicate.__dict__ = self.__dict__.copy() |
31 | | duplicate.dicts = duplicate.dicts[:] |
| 25 | duplicate = copy(super(BaseContext, self)) |
| 26 | duplicate.dicts = self.dicts[:] |
32 | 27 | return duplicate |
33 | 28 | |
34 | 29 | def __repr__(self): |
… |
… |
class BaseContext(object):
|
78 | 73 | return d[key] |
79 | 74 | return otherwise |
80 | 75 | |
| 76 | def new(self, values=None): |
| 77 | """ |
| 78 | Returns a new context with the same properties, but with only the |
| 79 | values given in 'values' stored. |
| 80 | """ |
| 81 | new_context = copy(self) |
| 82 | new_context._reset_dicts(values) |
| 83 | return new_context |
| 84 | |
81 | 85 | class Context(BaseContext): |
82 | 86 | "A stack container for variable context" |
83 | 87 | def __init__(self, dict_=None, autoescape=True, current_app=None, use_l10n=None): |
… |
… |
class Context(BaseContext):
|
88 | 92 | super(Context, self).__init__(dict_) |
89 | 93 | |
90 | 94 | def __copy__(self): |
91 | | duplicate = super(Context, self).__copy__() |
| 95 | duplicate = copy(super(Context, self)) |
92 | 96 | duplicate.render_context = copy(self.render_context) |
93 | 97 | return duplicate |
94 | 98 | |
… |
… |
class Context(BaseContext):
|
99 | 103 | self.dicts.append(other_dict) |
100 | 104 | return other_dict |
101 | 105 | |
102 | | def new(self, values=None): |
103 | | """ |
104 | | Returns a new Context with the same 'autoescape' value etc, but with |
105 | | only the values given in 'values' stored. |
106 | | """ |
107 | | return self.__class__(dict_=values, autoescape=self.autoescape, |
108 | | current_app=self.current_app, use_l10n=self.use_l10n) |
109 | | |
110 | 106 | class RenderContext(BaseContext): |
111 | 107 | """ |
112 | 108 | A stack container for storing Template state. |
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index 10c7a37..c326604 100644
a
|
b
|
class TemplateTagLoading(unittest.TestCase):
|
1640 | 1640 | settings.INSTALLED_APPS = ('tagsegg',) |
1641 | 1641 | t = template.Template(ttext) |
1642 | 1642 | |
| 1643 | class RequestContextTests(BaseTemplateResponseTest): |
| 1644 | |
| 1645 | def test_include_only(self): |
| 1646 | # Regression test for #15721 |
| 1647 | templates = { |
| 1648 | 'child': Template('{{ var|default:"none" }}'), |
| 1649 | } |
| 1650 | |
| 1651 | # Register a custom template loader. |
| 1652 | def test_template_loader(template_name, template_dirs=None): |
| 1653 | "A custom template loader that loads the unit-test templates." |
| 1654 | try: |
| 1655 | return (templates[template_name] , "test:%s" % template_name) |
| 1656 | except KeyError: |
| 1657 | raise template.TemplateDoesNotExist(template_name) |
| 1658 | |
| 1659 | old_template_loaders = loader.template_source_loaders |
| 1660 | loader.template_source_loaders = [test_template_loader] |
| 1661 | try: |
| 1662 | ctx = RequestContext(self.factory.get('/'), {'var': 'parent'}) |
| 1663 | self.assertEqual( |
| 1664 | template.Template('{% include "child" %}').render(ctx), |
| 1665 | 'parent' |
| 1666 | ) |
| 1667 | self.assertEqual( |
| 1668 | template.Template('{% include "child" only %}').render(ctx), |
| 1669 | 'none' |
| 1670 | ) |
| 1671 | finally: |
| 1672 | loader.template_source_loaders = old_template_loaders |
| 1673 | |
1643 | 1674 | if __name__ == "__main__": |
1644 | 1675 | unittest.main() |