{{{
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.
* 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>
* 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>
* Attachment "0001-Made-sure-self-can-t-be-overriden-in-Jinja.patch"
added.
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>
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>
* 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>
* 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>
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>
* has_patch: 0 => 1
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/24538#comment:8>
* 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>
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>
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>
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>