[Django] #24538: Rendering a Jinja template with a context containing 'self' fails

28 views
Skip to first unread message

Django

unread,
Mar 26, 2015, 12:45:19 AM3/26/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
---------------------------------+--------------------
Reporter: tim_heap | Owner: nobody
Type: Uncategorized | Status: new
Component: Template system | Version: 1.8rc1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
Rendering a DTL template with a context containing a 'self' key works.
Attempting the same with a Jinja template fails:

{{{
from django.shortcuts import render


def self_test(request):
# Assuming that there exists a DTL style template 'dtl.html',
# and a Jinja template named 'jinja.html':
render(request, 'dtl.html', {'self': 'test'}) # Works
render(request, 'jinja.html', {'self': 'test'}) # Fails
}}}

{{{
Internal Server Error: /self_test/
Traceback (most recent call last):
File "/home/vagrant/jinjatest_venv/local/lib/python2.7/site-
packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "/vagrant/jinjatest/views.py", line 6, in self_test
render(request, 'jinja.html', {'self': 'test'})
File "/home/vagrant/jinjatest_venv/local/lib/python2.7/site-
packages/django/shortcuts.py", line 67, in render
template_name, context, request=request, using=using)
File "/home/vagrant/jinjatest_venv/local/lib/python2.7/site-
packages/django/template/loader.py", line 99, in render_to_string
return template.render(context, request)
File "/home/vagrant/jinjatest_venv/local/lib/python2.7/site-
packages/django/template/backends/jinja2.py", line 63, in render
return self.template.render(**context)
TypeError: render() got multiple values for keyword argument 'self'
}}}

The Wagtail CMS uses 'self' as a key when rendering pages. This error
means that Wagtail can not be used in combination with Jinja templates.

The contents of the templates is irrelevant, and can be blank for testing.

--
Ticket URL: <https://code.djangoproject.com/ticket/24538>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 26, 2015, 3:01:15 AM3/26/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
---------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: assigned

Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by charettes):

* status: new => assigned
* severity: Normal => Release blocker
* needs_better_patch: => 0
* needs_tests: => 0
* owner: nobody => charettes
* needs_docs: => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Comment:

It should be possible to
[http://jinja2.readthedocs.org/en/latest/api.html#jinja2.Template.render
pass contex` as an arg] to `render()` instead of `**kwargs`.

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:1>

Django

unread,
Mar 26, 2015, 3:19:41 AM3/26/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
---------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: closed

Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution: invalid

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by charettes):

* status: assigned => closed
* resolution: => invalid


Comment:

Unfortunately it looks like a limitation of Jinja2 itself:

{{{#!python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from jinja2 import Environment
>>> env = Environment()
>>> template = env.from_string('{{ self }}')
>>> template.render({'self': 'Self!'})
u'<TemplateReference None>'
}}}

I think the `TypeError` raised here makes more sense here than silently
ignoring the provided `self` context variable.

Attaching a test case leading to this conclusion.

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:2>

Django

unread,
Mar 26, 2015, 3:20:04 AM3/26/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
---------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: closed
Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution: invalid
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by charettes):

* Attachment "0001-Made-sure-self-can-t-be-overriden-in-Jinja.patch"
added.

Django

unread,
Mar 26, 2015, 4:56:12 AM3/26/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
---------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: closed
Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution: invalid
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------

Comment (by aaugustin):

This patch would be excellent, if only the test passed ;-)

Could you file a bug against Jinja2?

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:3>

Django

unread,
Mar 26, 2015, 7:55:10 PM3/26/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
---------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: closed
Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution: invalid
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------

Comment (by tim_heap):

Upon further reading, it appears that 'self' is a special variable in
Jinja. [http://jinja.pocoo.org/docs/dev/templates/#child-template From the
Jinja docs]:

If you want to print a block multiple times, you can, however, use the
special self variable and call the block with that name:
{{{#!jinja
<title>{% block title %}{% endblock %}</title>
<h1>{{ self.title() }}</h1>
{% block body %}{% endblock %}
}}}

Changing Jinja to suit this requirement is not reasonable, so I will log a
bug with Wagtail CMS to rename the 'self' variable to something that can
work.

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:4>

Django

unread,
Mar 27, 2015, 9:17:37 AM3/27/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
---------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: closed
Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution: wontfix

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by aaugustin):

* resolution: invalid => wontfix


Comment:

Thanks for the analysis. Indeed this appears to be the right solution.

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:5>

Django

unread,
Mar 30, 2015, 6:30:29 PM3/30/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
---------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: new

Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution:

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by tim_heap):

* status: closed => new
* resolution: wontfix =>


Comment:

Following some discussions on the [https://groups.google.com/forum/#!topic
/wagtail-developers/MxE6ImpVPCs Wagtail developer mailing list], we came
up with a potential solution: use both 'self' and 'page' in the context,
so that either template engine can be used, but without breaking backwards
compatibility with previous Wagtail versions using self, and without
breaking third party plugins.

As Jinja will silently ignore a self context variable in favour of its
own, this duplicate variable approach will work. Django just has to be
patched so that it does not throw an error when self is used. I will
construct a pull request fixing this shortly.

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:6>

Django

unread,
Mar 31, 2015, 12:40:11 AM3/31/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
---------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: new

Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------

Comment (by tim_heap):

A pull request has been created for this:
https://github.com/django/django/pull/4415

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:7>

Django

unread,
Apr 1, 2015, 9:18:23 AM4/1/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
-------------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: new

Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:8>

Django

unread,
Apr 1, 2015, 12:28:11 PM4/1/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
-------------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: closed

Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"4ea1909d3c420ba1fbdbf7221cad518d43aef885" 4ea1909]:
{{{
#!CommitTicketReference repository=""
revision="4ea1909d3c420ba1fbdbf7221cad518d43aef885"
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.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:9>

Django

unread,
Apr 1, 2015, 12:28:26 PM4/1/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
-------------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: closed
Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"ff8eabc5ccb7af928856910c8af72d84346821e9" ff8eabc]:
{{{
#!CommitTicketReference repository=""
revision="ff8eabc5ccb7af928856910c8af72d84346821e9"
[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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:10>

Django

unread,
Apr 1, 2015, 1:09:20 PM4/1/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
-------------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: closed
Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"a184a99123e4dc4c2a90c20e1604e5d301cd76bf" a184a99]:
{{{
#!CommitTicketReference repository=""
revision="a184a99123e4dc4c2a90c20e1604e5d301cd76bf"
Refs #24538 -- Simplified a test per Aymeric's feedback.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:11>

Django

unread,
Apr 1, 2015, 1:20:11 PM4/1/15
to django-...@googlegroups.com
#24538: Rendering a Jinja template with a context containing 'self' fails
-------------------------------------+-------------------------------------
Reporter: tim_heap | Owner: charettes
Type: Bug | Status: closed
Component: Template system | Version: 1.8rc1
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"5a4f95805f9b4f709cfcac53a453b59bd40a9946" 5a4f9580]:
{{{
#!CommitTicketReference repository=""
revision="5a4f95805f9b4f709cfcac53a453b59bd40a9946"
[1.8.x] Refs #24538 -- Simplified a test per Aymeric's feedback.

Backport of a184a99123e4dc4c2a90c20e1604e5d301cd76bf from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:12>

Reply all
Reply to author
Forward
0 new messages