Friday, July 30, 2010

Re: UnicodeEncodeError

On Jul 29, 11:27 pm, Eric <ericbr...@gmail.com> wrote:
> I am getting this error in the Django Administration section when I
> add an entry for a Dashboard object.
>
> When I return to the item in the Admin section, the record has been
> added and it displays correctly in the list of Dashboard objects. When
> I click on it, it shows correctly. Only when I add a new one or save
> one does that happend. And, I noticed that the history is not
> populated.
>
> The traceback is:
>
> Environment:
>
> Request Method: POST
> Request URL:http://127.0.0.1:8000/admin/dashboard/dashboard/1/
> Django Version: 1.2.1
> Python Version: 2.6.2
> Installed Applications:
> ['django.contrib.auth',
>  'django.contrib.contenttypes',
>  'django.contrib.sessions',
>  'django.contrib.sites',
>  'django.contrib.messages',
>  'django.contrib.admin',
>  'ManagementReview.dashboard']
> Installed Middleware:
> ('django.middleware.common.CommonMiddleware',
>  'django.contrib.sessions.middleware.SessionMiddleware',
>  'django.middleware.csrf.CsrfViewMiddleware',
>  'django.contrib.auth.middleware.AuthenticationMiddleware',
>  'django.contrib.messages.middleware.MessageMiddleware')
>
> Traceback:
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/core/handlers/base.py" in get_response
>   100.                     response = callback(request,
> *callback_args, **callback_kwargs)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/contrib/admin/options.py" in wrapper
>   239.                 return self.admin_site.admin_view(view)(*args,
> **kwargs)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
>   76.                     response = view_func(request, *args,
> **kwargs)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/views/decorators/cache.py" in
> _wrapped_view_func
>   69.         response = view_func(request, *args, **kwargs)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/contrib/admin/sites.py" in inner
>   190.             return view(request, *args, **kwargs)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/utils/decorators.py" in _wrapper
>   21.             return decorator(bound_func)(*args, **kwargs)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
>   76.                     response = view_func(request, *args,
> **kwargs)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/utils/decorators.py" in bound_func
>   17.                 return func(self, *args2, **kwargs2)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/db/transaction.py" in
> _commit_on_success
>   299.                     res = func(*args, **kw)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/contrib/admin/options.py" in
> change_view
>   901.                 self.log_change(request, new_object,
> change_message)
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/contrib/admin/options.py" in log_change
>   443.             object_repr     = force_unicode(object),
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/utils/encoding.py" in force_unicode
>   66.                 s = unicode(s)
> File "/Users/Eric/Documents/DjangoProjects/ManagementReview/../
> ManagementReview/dashboard/models.py" in __unicode__
>   47.         return u"%s" % self.name.decode('utf-8')
> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/encodings/utf_8.py" in decode
>   16.     return codecs.utf_8_decode(input, errors, True)
>
> Exception Type: UnicodeEncodeError at /admin/dashboard/dashboard/1/
> Exception Value: ('ascii', u"Eric's Nifty D\xe4shboard Entry", 14, 15,
> 'ordinal not in range(128)')
>
> The model looks like this:
>
> class Dashboard(models.Model):
>     name = models.CharField("Dashboard name",max_length=100)
>     fiscal_year = models.IntegerField("Fiscal year")
>     region = models.CharField("Region", max_length=30)
>     review_date = models.DateField("Review date")
>     date_completed = models.DateField("Date Completed")
>
>     def __unicode__(self):
>         return u"%s" % self.name.decode('utf-8')
>
> Can somebody help me figure out what is going on here?
>
> Thanks
> Eric

This wouldn't necessarily cause that error, but your __unicode__
method is unnecessarily complicated. All model elements retrieved from
the db are already unicode, so there's no reason to 'decode' from
utf-8. The method should simply return self.name.

In fact, on reflection this could well be the cause of your problem -
because you're calling .decode() on something that is already unicode,
Python will try to encode it to bytes first - and since you haven't
specified an encoding, it will default to ASCII, hence the encoding
error.
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment