Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#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)

ticket15535_r15708.diff (1.6 KB ) - added by Stephen Burrows 13 years ago.

Download all attachments as: .zip

Change History (3)

by Stephen Burrows, 13 years ago

Attachment: ticket15535_r15708.diff added

comment:1 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: newclosed

In [15709]:

Fixed #15535 -- Stopped the blocktrans template tag from raising a KeyError if an included variable can't be found in the context. Thanks, melinath.

comment:2 by Jannis Leidel, 13 years ago

In [15710]:

[1.2.X] Fixed #15535 -- Stopped the blocktrans template tag from raising a KeyError if an included variable can't be found in the context. Thanks, melinath.

Backport from trunk (r15709).

Note: See TracTickets for help on using tickets.
Back to Top