diff options
| author | Tim Heap <tim@timheap.me> | 2015-03-31 15:37:17 +1100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-04-01 12:27:52 -0400 |
| commit | ff8eabc5ccb7af928856910c8af72d84346821e9 (patch) | |
| tree | adf3722c40f4a35f368c7c3f04a75932f61e6d98 | |
| parent | 5483c66f85cf67d52d92755ce550de1a4d3c5715 (diff) | |
[1.8.x] Fixed #24538 -- Allowed self in Jinja context
Rendering a Jinja template with self in the context threw an error.
While self is a reserved variable in Jinja, including self in the
context is not an error, so Django should respect that.
Backport of 4ea1909d3c420ba1fbdbf7221cad518d43aef885 from master
| -rw-r--r-- | django/template/backends/jinja2.py | 2 | ||||
| -rw-r--r-- | tests/template_backends/test_jinja2.py | 18 |
2 files changed, 19 insertions, 1 deletions
diff --git a/django/template/backends/jinja2.py b/django/template/backends/jinja2.py index b26cb391f2..0863773ddf 100644 --- a/django/template/backends/jinja2.py +++ b/django/template/backends/jinja2.py @@ -60,4 +60,4 @@ class Template(object): context['request'] = request context['csrf_input'] = csrf_input_lazy(request) context['csrf_token'] = csrf_token_lazy(request) - return self.template.render(**context) + return self.template.render(context) diff --git a/tests/template_backends/test_jinja2.py b/tests/template_backends/test_jinja2.py index 6d67d10893..17cdb05df0 100644 --- a/tests/template_backends/test_jinja2.py +++ b/tests/template_backends/test_jinja2.py @@ -27,3 +27,21 @@ class Jinja2Tests(TemplateStringsTests): engine_class = Jinja2 backend_name = 'jinja2' options = {'keep_trailing_newline': True} + + def test_self_context(self): + """ + #24538 -- Using 'self' in the context should not throw errors + """ + engine = Jinja2({ + 'DIRS': [], + 'APP_DIRS': False, + 'NAME': 'django', + 'OPTIONS': {}, + }) + + # self will be overridden to be a TemplateReference, so the self + # variable will not come through. Attempting to use one though should + # not throw an error. + template = engine.from_string('hello {{ foo }}!') + content = template.render(context={'self': 'self', 'foo': 'world'}) + self.assertEqual(content, 'hello world!') |
