#24538 closed Bug (fixed)
Rendering a Jinja template with a context containing 'self' fails
Reported by: | Tim Heap | Owned by: | Simon Charette |
---|---|---|---|
Component: | Template system | Version: | 1.8rc1 |
Severity: | Release blocker | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Attachments (1)
Change History (13)
comment:1 by , 10 years ago
Owner: | changed from | to
---|---|
Severity: | Normal → Release blocker |
Status: | new → assigned |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:2 by , 10 years ago
Resolution: | → invalid |
---|---|
Status: | assigned → closed |
Unfortunately it looks like a limitation of Jinja2 itself:
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.
by , 10 years ago
Attachment: | 0001-Made-sure-self-can-t-be-overriden-in-Jinja.patch added |
---|
comment:3 by , 10 years ago
This patch would be excellent, if only the test passed ;-)
Could you file a bug against Jinja2?
comment:4 by , 10 years ago
Upon further reading, it appears that 'self' is a special variable in Jinja. 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:
<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.
comment:5 by , 10 years ago
Resolution: | invalid → wontfix |
---|
Thanks for the analysis. Indeed this appears to be the right solution.
comment:6 by , 10 years ago
Resolution: | wontfix |
---|---|
Status: | closed → new |
Following some discussions on the 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.
comment:7 by , 10 years ago
A pull request has been created for this: https://github.com/django/django/pull/4415
comment:8 by , 10 years ago
Has patch: | set |
---|---|
Triage Stage: | Accepted → Ready for checkin |
It should be possible to pass contex` as an arg to
render()
instead of**kwargs
.