#15535 closed (fixed)
{% blocktrans %} tag raises KeyErrors while rendering
Reported by: | Stephen Burrows | Owned by: | nobody |
---|---|---|---|
Component: | Internationalization | Version: | dev |
Severity: | Keywords: | blocktrans | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Blocktrans allows variables to be nested inside of it; however, if one of those variables is not defined in the context, a KeyError will be raised during the render of the block node. Ex:
{% blocktrans %}{{ not_defined }}{% endblocktrans %}
This can be seen by using the manage.py shell:
>>> from django.template import Template, Context >>> t = Template("{% load i18n %}{% blocktrans %}{{ not_defined }}{% endblocktrans %}") >>> c = Context() >>> t.render(c) Traceback (most recent call last): File "<console>", line 1, in <module> File ".../django/template/base.py", line 123, in render return self._render(context) File ".../django/template/base.py", line 117, in _render return self.nodelist.render(context) File ".../django/template/base.py", line 744, in render bits.append(self.render_node(node, context)) File ".../django/template/debug.py", line 73, in render_node result = node.render(context) File ".../django/templatetags/i18n.py", line 116, in render data = dict([(v, _render_value_in_context(context[v], context)) for v in vars]) File ".../django/template/context.py", line 60, in __getitem__ raise KeyError(key) TemplateSyntaxError: Caught KeyError while rendering: not_defined >>>
According to the docs, template tags should never raise errors during their render method. [1] My patch attempts to address this by adding the empty string as a failobj for each attempt to get a variable from the context. I'm not familiar with how exactly the internationalization framework works, so this may be an inappropriate way of handling things; however, no tests fail after adding the patch.
[1] http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-the-renderer
Attachments (1)
Change History (3)
by , 14 years ago
Attachment: | ticket15535_r15708.diff added |
---|
comment:1 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
In [15709]: