UnicodeEncodeError

178 views
Skip to first unread message

rodrigo

unread,
Dec 26, 2007, 12:47:12 PM12/26/07
to Django users
I am getting a "UnicodeEncodeError" when trying to add data containing
accented characters (or 'ñ') via the admin interface.
I am using the latest revision of Django via svn, and a mysql database
with "DEFAULT CHARACTER SET utf8".
Here's the odd part: The data does get added to the database, the
error seems to be related to the admin interface's forms.
In this case, I created an 'author' named : Niño Niñó. This gives me
the error reproduced below. However, if I go into the list view for
Authors, it shows up.If I try to modify Niño, I get a
UnicodeEncodeError. I also get the same error if I try to create a new
Publication (that has Author as a ManyToManyField) I also get the
UnicodeEncodeError.
If I delete the offending entry in the DB, the errors go away.

Any help is of course appreciated,

Rodrigo

------
The error:

UnicodeEncodeError at /admin/revistas/articulo/add/
'ascii' codec can't encode character u'\xd1' in position 2: ordinal
not in range(128)

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/revistas/autor/add/
Exception Type: UnicodeEncodeError
Exception Value: 'ascii' codec can't encode character u'\xd1' in
position 2: ordinal not in range(128)
Exception Location: C:\Python24\lib\site-packages\django\utils
\encoding.py in force_unicode, line 53
Python Executable: C:\Python24\python.exe
Python Version: 2.4.3
Python Path: (removed)
Unicode error hint

The string that could not be encoded/decoded was: NIÑÓ, Niñ

-----
The last entry of the trackback is:

C:\Python24\lib\site-packages\django\utils\encoding.py in
force_unicode

46. if strings_only and isinstance(s, (types.NoneType, int, long,
datetime.datetime, datetime.date, datetime.time, float)):
47. return s
48. try:
49. if not isinstance(s, basestring,):
50. if hasattr(s, '__unicode__'):
51. s = unicode(s)
52. else:

53. s = unicode(str(s), encoding, errors) ...

54. elif not isinstance(s, unicode):
55. # Note: We use .decode() here, instead of unicode(s, encoding,
56. # errors), so that if s is a SafeString, it ends up being a
57. # SafeUnicode at the end.
58. s = s.decode(encoding, errors)
59. except UnicodeDecodeError, e:

▼ Local vars
Variable Value
encoding
'utf-8'
errors
'strict'
s
<Autor: NIÑO, Ñód>
strings_only
False





Karen Tracey

unread,
Dec 26, 2007, 1:05:59 PM12/26/07
to django...@googlegroups.com
On Dec 26, 2007 12:47 PM, rodrigo <rodri...@gmail.com> wrote:

I am getting a "UnicodeEncodeError" when trying to add data containing
accented characters (or 'ñ') via the admin interface.
I am using the latest revision of Django via svn, and a mysql database
with "DEFAULT CHARACTER SET utf8".
Here's the odd part: The data does get added to the database, the
error seems to be related to the admin interface's forms.
In this case, I created an 'author' named : Niño Niñó. This gives me
the error reproduced below. However, if I go into the list view for
Authors, it shows up.If I try to modify Niño, I get a
UnicodeEncodeError. I also get the same error if I try to create a new
Publication (that has Author as a ManyToManyField) I also get the
UnicodeEncodeError.
If I delete the offending entry in the DB, the errors go away.

Any help is of course appreciated,

Rodrigo

------
The error:
[details snipped]

Does your author model have an __str__ method instead of a __unicode__ method?  That would cause the error you are reporting.  Changing it to __unicode__ will fix the problem.

Karen

Rodrigo Culagovski

unread,
Dec 26, 2007, 1:27:45 PM12/26/07
to Django users
Thanks so much, that did the trick.

Rodrigo

On Dec 26, 3:05 pm, "Karen Tracey" <kmtra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages