#5884: UnicodeDecodeError attempting to render VariableDoesNotExist as a string
-------------------------------------------------+--------------------------
   Reporter:  Karen Tracey <[EMAIL PROTECTED]>  |                Owner:  nobody 
        
     Status:  reopened                           |            Component:  
Template system
    Version:  SVN                                |           Resolution:        
         
   Keywords:                                     |                Stage:  
Accepted       
  Has_patch:  1                                  |           Needs_docs:  0     
         
Needs_tests:  0                                  |   Needs_better_patch:  0     
         
-------------------------------------------------+--------------------------
Comment (by Karen Tracey <[EMAIL PROTECTED]>):

 With the patch, the !VariableDoesNotExist exception still gets suppressed,
 only now by a !DjangoUnicodeDecodeError, which at least gives a hint that
 the first problem encountered was the !VariableDoesNotExist:

 {{{

 >>> from django.template import Context, loader
 >>> t = loader.get_template('simple.tmpl')
 >>> from crossword.models import Authors
 >>> a = Authors.objects.get(pk=535)
 >>> a
 <Authors: Sue de Nîmes>
 >>> t.render(Context({'item_list': [1,2,3], 'xtra': a}))
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/homedir/django/trunk/django/template/__init__.py", line 173, in
 render
     return self.nodelist.render(context)
   File "/homedir/django/trunk/django/template/__init__.py", line 748, in
 render
     bits.append(self.render_node(node, context))
   File "/homedir/django/trunk/django/template/debug.py", line 78, in
 render_node
     wrapped = TemplateSyntaxError(u'Caught an exception while rendering:
 %s' % force_unicode(e, errors='replace'))
   File "/homedir/django/trunk/django/utils/encoding.py", line 60, in
 force_unicode
     raise DjangoUnicodeDecodeError(s, *e.args)
 DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
 213: ordinal not in range(128). You passed in VariableDoesNotExist()
 (<class 'django.template.VariableDoesNotExist'>)

 }}}

 I had tried similar fixes when I first ran into this and for some reason
 couldn't find one that worked.  However I took another look today and now
 I find that if I liberally sprinkle force_unicode down in
 !VariableDoesNotExist that fixes the !UnicodeDecode error and the real
 !VariableDoesNotExist is preserved:

 {{{

 >>> from django.template import Context, loader
 >>> t = loader.get_template('simple.tmpl')
 >>> from crossword.models import Authors
 >>> a = Authors.objects.get(pk=535)
 >>> t.render(Context({'item_list': [1,2,3], 'xtra': a}))
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/homedir/django/trunk/django/template/__init__.py", line 173, in
 render
     return self.nodelist.render(context)
   File "/homedir/django/trunk/django/template/__init__.py", line 748, in
 render
     bits.append(self.render_node(node, context))
   File "/homedir/django/trunk/django/template/debug.py", line 81, in
 render_node
     raise wrapped
 TemplateSyntaxError: Caught an exception while rendering: Failed lookup
 for key [rowvar] in u"[{'forloop': {'revcounter0': 0, 'last': True,
 'counter': 3, 'parentloop': {}, 'revcounter': 1, 'counter0': 2, 'first':
 False}, u'item': 3, u'rowcolors': u'odd'}, {'item_list': [1, 2, 3],
 'xtra': <Authors: Sue de N\xeemes>}]"

 Original Traceback (most recent call last):
   File "/homedir/django/trunk/django/template/debug.py", line 71, in
 render_node
     result = node.render(context)
   File "/homedir/django/trunk/django/template/defaulttags.py", line 149,
 in render
     nodelist.append(node.render(context))
   File "/homedir/django/trunk/django/template/defaulttags.py", line 47, in
 render
     value = Variable(value).resolve(context)
   File "/homedir/django/trunk/django/template/__init__.py", line 656, in
 resolve
     value = self._resolve_lookup(context)
   File "/homedir/django/trunk/django/template/__init__.py", line 709, in
 _resolve_lookup
     raise VariableDoesNotExist("Failed lookup for key [%s] in %r", (bit,
 current)) # missing attribute
 VariableDoesNotExist

 }}}

 I'm not sure all of the force_unicodes I put in are necessary, and I don't
 know if there could ever be anything in a context that would not support
 having force_unicode applied to it, so someone with more of a clue in this
 area of code should verify the fix is OK.  I also still don't think it is
 necessary to print out the whole context in this situation, but if it is
 desired to keep that behavior then I've got a possible fix for the problem
 that does it.  I'll attach that diff to this ticket.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/5884#comment:5>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to