Unicode error in __unicode__(self) function

73 views
Skip to first unread message

rmschne

unread,
May 8, 2017, 12:10:15 PM5/8/17
to Django users
I have a Django setup that has worked for a very long time. Yesterday I upgraded from Django 1.10 to 1.11.1 and am getting the error:

    raise DjangoUnicodeDecodeError(s, *e.args)
django.utils.encoding.DjangoUnicodeDecodeErrorJón
: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128). You passed in <Meetingattendee: [Bad Unicode data]> (<class 'soc_util.models.Meetingattendee'>)

when in the code simply printing the "Meetingattendee" object with

def __unicode__(self):
        print self.fname
        return self.fname

The above is a simplified version. The error first cropped up on the more complex version

def __unicode__(self):
        s=u'%s: %s, %s %s, %-12s, %s %s, %s, INV:%s, PO:%s, ItemCode:%s' % (self.id,self.meeting.date.strftime("%d-%b-%Y"),self.table,self.seat,\
            self.attendeestatus.status,self.fname, self.lname, self.affiliation, \
            self.invoiceno,self.ponumber,self.itemcode)
        return s
 
where I eventually determined that the code was not working when working the field self.fname.

self.fname fails when it is "Jón".  Works fin with "Jon", of course.  

I have not tried to revert back to Django 1.10, but i guess that's the next step unless there is something in Django 1.11.1 that was changed that affects this. I can't find anything mentioned in the release notes.

Tom Evans

unread,
May 8, 2017, 12:22:42 PM5/8/17
to django...@googlegroups.com
What data is "Jón"?

Is it u'J\xf3n' or something else? (print repr(self.fname))

Cheers

Tom
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4d37cc78-7fe9-41b6-bcba-eb788465e54f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

rmschne

unread,
May 8, 2017, 2:43:12 PM5/8/17
to Django users
Yes, it is the first name of a person with a European name, who it's got that "ó" character. My understanding that it is Unicode. The variable prints ok in the rest of the code to the screen and in Django templates, but this __unicode_(self) method fails. 

Melvyn Sopacua

unread,
May 9, 2017, 4:42:32 AM5/9/17
to django...@googlegroups.com

On Monday 08 May 2017 09:10:15 rmschne wrote:

> I have a Django setup that has worked for a very long time. Yesterday

> I upgraded from Django 1.10 to 1.11.1 and am getting the error:

>

> raise DjangoUnicodeDecodeError(s, *e.args)

> django.utils.encoding.DjangoUnicodeDecodeErrorJón

>

> : 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in

>

> range(128).

 

I'm sorry - but this isn't your real error.

You will see it in your WSGI daemon logs once you set DEBUG to false.

 

This error is caused by Django trying to print the error using ascii codec for the debug output (I haven't chased it down any further and I should really report it).

 

As said, your real error is somewhere else and that will probably make more sense.

 

--

Melvyn Sopacua

rmschne

unread,
May 9, 2017, 9:00:31 AM5/9/17
to Django users
 

I'm sorry - but this isn't your real error.

You will see it in your WSGI daemon logs once you set DEBUG to false.

 

This error is caused by Django trying to print the error using ascii codec for the debug output (I haven't chased it down any further and I should really report it).

 

As said, your real error is somewhere else and that will probably make more sense.

 

--

Melvyn Sopacua


Melvyn,

Thanks. I understand what you are saying. Obvious, but I didn't notice that. However, I do think it's a Django bug. This app is not running on a web server so there is no WSGI daemon.  Do you have a suggestion on what I can do to get a better detection of the error to enable reporting it? 

Vijay Khemlani

unread,
May 9, 2017, 9:28:38 AM5/9/17
to django...@googlegroups.com
Paste the whole stack traceof the error
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b3ed676d-811a-4c53-b176-bd460f5dd54a%40googlegroups.com.

m712 - Developer

unread,
May 9, 2017, 12:57:34 PM5/9/17
to Melvyn Sopacua, django...@googlegroups.com

On May 9, 2017 11:42 AM, Melvyn Sopacua <m.r.s...@gmail.com> wrote:
> I'm sorry - but this isn't your real error.

Your error is in another castle?

Melvyn Sopacua

unread,
May 10, 2017, 4:39:37 AM5/10/17
to django...@googlegroups.com

On Tuesday 09 May 2017 06:00:31 rmschne wrote:

> > I'm sorry - but this isn't your real error.

> >

> > You will see it in your WSGI daemon logs once you set DEBUG to

> > false.

> >

> >

> >

> > This error is caused by Django trying to print the error using ascii

> > codec for the debug output (I haven't chased it down any further

> > and I should really report it).

> >

> >

> >

> > As said, your real error is somewhere else and that will probably

> > make more sense.

> >

> >

> >

> >

> > Melvyn Sopacua

>

> Melvyn,

>

> Thanks. I understand what you are saying. Obvious, but I didn't notice

> that. However, I do think it's a Django bug. This app is not running

> on a web server so there is no WSGI daemon.

 

Ah yes! That triggers a memory. Since `python manage.py runserver` runs on terminal the codec is set to ascii (even when using a UTF-8 locale). So set debug to false and see what comes up in the console.

Another way is to temporarily change the data to ascii. It should then still yield an error and that is your real one.

 

> Do you have a suggestion

> on what I can do to get a better detection of the error to enable

> reporting it?

 

Since runserver crashes, your options are limited. You could maybe construct a test case using django.test.Client and see what is triggered.

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages