id,summary,reporter,owner,description,type,status,component,version,severity,resolution,keywords,cc,stage,has_patch,needs_docs,needs_tests,needs_better_patch,easy,ui_ux 5884,UnicodeDecodeError attempting to render VariableDoesNotExist as a string,Karen Tracey ,nobody,"Given a simple template (simple.tmpl): {{{ {% for item in item_list %} {% endfor %}
{{ item }}
}}} Attempting to render the template using a context that does not include the variable rowvar produces a TemplateSyntaxError resulting from raising of a VariableDoesNotExist, as expected: {{{ >>> from django.template import Context, loader >>> t = loader.get_template('simple.tmpl') >>> t.render(Context({'item_list': [1,2,3]})) Traceback (most recent call last): File """", line 1, in File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 174, in render return self.nodelist.render(context) File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 792, in render bits.append(self.render_node(node, context)) File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 820, in render_node raise wrapped TemplateSyntaxError: Caught an exception while rendering: Failed lookup for key [rowvar] in [{'forloop': {'parentloop': {}, 'last': True, 'counter': 3, 'revcounter0': 0, 'revcounter': 1, 'counter0': 2, 'first': False}, u'item': 3, u'rowcolors': u'odd'}, {'item_list': [1, 2, 3]}] Original Traceback (most recent call last): File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 810, in render_node result = node.render(context) File ""/homedir/django/newforms-admin/django/template/defaulttags.py"", line 135, in render nodelist.append(node.render(context)) File ""/homedir/django/newforms-admin/django/template/defaulttags.py"", line 33, in render value = Variable(value).resolve(context) File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 704, in resolve return self._resolve_lookup(context) File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 754, in _resolve_lookup raise VariableDoesNotExist(""Failed lookup for key [%s] in %r"", (bit, current)) # missing attribute VariableDoesNotExist: Failed lookup for key [rowvar] in [{'forloop': {'parentloop': {}, 'last': True, 'counter': 3, 'revcounter0': 0, 'revcounter': 1, 'counter0': 2, 'first': False}, u'item': 3, u'rowcolors': u'odd'}, {'item_list': [1, 2, 3]}] }}} If we now make the same error, but also include in the context an object whose representation includes non-ASCII data, the TemplateSyntaxError error is masked by a UnicodeDecodeError when the VariableDoesNotExist object attempts to return a string that include a representation of the context: {{{ >>> from crossword.models import Authors >>> a = Authors.objects.get(pk=535) >>> a >>> t.render(Context({'item_list': [1,2,3], 'xtra': a})) Traceback (most recent call last): File """", line 1, in File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 174, in render return self.nodelist.render(context) File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 792, in render bits.append(self.render_node(node, context)) File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 817, in render_node wrapped = TemplateSyntaxError('Caught an exception while rendering: %s' % e) File ""/homedir/django/newforms-admin/django/template/__init__.py"", line 130, in __str__ return self.msg % self.params UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 213: ordinal not in range(128) >>> }}} My Authors model contains the following __unicode__ method: {{{ def __unicode__(self): return u'%s' % self.Author }}} and the Author field is a CharField: {{{ Author = models.CharField(unique=True, maxlength=50) }}} I'm not sure what the correct fix is, but I'd be willing to create a patch if someone would point me in the right direction for how to fix it. One possibility would be to stop including the string representation of the context in the error message. I don't really see how it is useful...the key being sought isn't in there, what's the value of showing all the stuff (and it can be an awful lot of stuff, repeated three times on the debug page) that is in there? ",,closed,Template system,dev,,fixed,,,Ready for checkin,1,0,0,0,0,0