#3924: Caught an exception while rendering: 'ascii' codec can't decode byte 0xc3 in position 8: ordinal not in range(128) --------------------------------------------+------------------------------- Reporter: ruben.pe...@gmail.com | Owner: hugo Status: closed | Milestone: Component: django.contrib.admin | Version: 1.0 Resolution: fixed | Keywords: Stage: Accepted | Has_patch: 0 Needs_docs: 0 | Needs_tests: 0 Needs_better_patch: 0 | --------------------------------------------+------------------------------- Comment (by kmtracey):
Yes, Python's behavior here can cause some confusion. Note `"%s" % x` may evaluate to either Unicode or a bytestring depending on the type of x: {{{ #!python >>> u = u"Unicode!" >>> b = 'Bytestring' >>> "%s" % u u'Unicode!' >>> "%s" % b 'Bytestring' }}} This is why you often won't see a problem with your Visitor `__unicode__` as originally coded: usually the `lastName` and `firstName` attributes will be Unicode values, so `"%s %s" % (self.lastName,self.firstName)` will evaluate to a Unicode value and you won't see a problem. For more complicated situations, say a class that supports both `__str__` and `__unicode__`, whether or not you specify `u''` on the interpolation will control what type of result you get: {{{ #!python >>> class Thing(object): ... def __init__(self, x): ... self.x = x ... def __str__(self): ... return self.x.encode('utf-8') ... def __unicode__(self): ... return self.x ... >>> t = Thing(u'\u0101') >>> "%s" % t '\xc4\x81' >>> u"%s" % t u'\u0101' }}} This is where your Visit `__unicode__` method as originally coded has a problem. `self.visitor` has both `__str__` and `__unicode__` methods, so unless you force the `__unicode__` one to be called by using `u''` for the interpolation, the result will be a bytestring, and if that contains non- ascii chars then the automatic coercion to unicode using the ascii codec will generate an exception. {{{ #!python >>> unicode("%s" % t) Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128) >>> }}} So yes, sometimes it seems like you don't necessarily absolutely need the `u''` for the `__unicode__` return value. But figuring out when exactly you need it and when you don't can be complicated so it's a good idea to use it as a matter of routine. -- Ticket URL: <http://code.djangoproject.com/ticket/3924#comment:30> Django <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 django-updates@googlegroups.com To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-updates?hl=en -~----------~----~----~----~------~----~------~--~---