Opened 8 years ago

Closed 7 years ago

#14591 closed (invalid)

UnicodeDecodeError when trying to render the label of a field

Reported by: panosl Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Keywords: unicode
Cc: rasca7@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


An error is thrown if you specify a label for a field, ie:

from django.db import models
from django.utils.translation import gettext_lazy as _

class MyModel(models.Model):
        name = models.CharField(_('name'), max_length=50)

If you don't specify a list_display in the for that model, it will just show all of the fields. This is were this error occurs:

Caught UnicodeDecodeError while rendering: ('ascii', '\xce\xb3\xcf\x81\xce\xb1\xce\xbc\xce\xbc\xce\xae \xcf\x80\xce\xb1\xcf\x81\xce\xb1\xce\xb3\xce\xb3\xce\xb5\xce\xbb\xce\xb5\xce\xaf\xce\xb1\xcf\x82', 0, 1, 'ordinal not in range(128)')

A way to fix it is to edit django.contrib/admin/, ModelAdmin class, set list_display to __unicode__:

class ModelAdmin(BaseModelAdmin):
    "Encapsulates all admin options and functionality for a given model."

    #list_display = ('__str__',)
    list_display = ('__unicode__',)

I'm not adding it as a patch right now, not sure if it's the right way to fix it. Maybe we should use force_unicode, but fixing it like above, doesn't seem to break anything.

Attachments (1)

options.patch (468 bytes) - added by panosl 8 years ago.

Download all attachments as: .zip

Change History (8)

Changed 8 years ago by panosl

Attachment: options.patch added

comment:1 Changed 8 years ago by panosl

Has patch: set

Added a patch after all, might be easier to try it out that way.

comment:2 Changed 8 years ago by rasca

Resolution: worksforme
Status: newclosed

Just tried this and it works for me with no problems..

comment:3 in reply to:  2 Changed 8 years ago by panosl

Resolution: worksforme
Status: closedreopened

Replying to rasca:

Just tried this and it works for me with no problems..

Did you actually try to add a translation for the string? I forgot to state that explicitly, sorry.

Try setting a greek locale for example and setting the str to 'όνομα'.

comment:4 Changed 8 years ago by rasca

Cc: rasca7@… added

Just tried it in Django 1.2, 1.2.3 and trunk and working fine in all of them (python 2.6.6)...

This is what I did: copied your first snippet to,
then added to,
set the LANGAUGE_CODE to es-ar,
makemessages -l es_AR,
modified the .po to msgid "name" msgstr "áéíóñúόνομα",
compilemessages, runserver and entered the admin and it shows "Áéíóñúόνομα:" before the name field in the changeform

It works fine.. can you reproduce the problem in simple steps like this ones?

comment:5 Changed 8 years ago by panosl

Hey Rasca, please don't hate me :)

I was mistaken, since the error was coming from the verbose_name, rather than the translation of the field, 'cause of course when you don't set any list options, the only <th> that shows up is the model's name.

I've made a demo project/app for you to see first hand.

Download it from

admin credentials are: admin/pass

visit the admin page for uerror app, and you'll see the error.

Sorry for wasting your time prior to this, I should have done this from the start, but I've nailed it for you (hopefully), let me know if you need any other info.

comment:6 Changed 8 years ago by rasca

Needs tests: set
Triage Stage: UnreviewedDesign decision needed

Okey, I get the error now. It wont happen if you use ugettext_lazy instead of gettext_lazy.

I'm marking it as DDN, cause I'm not sure if this should be accepted or not to preserve consistency.

comment:7 Changed 7 years ago by Ramiro Morales

Resolution: invalid
Status: reopenedclosed

Closing because, as rasca pointed, the OP should be using ugettext_lazy instead of gettext_lazy as described in the documentation (

Note: See TracTickets for help on using tickets.
Back to Top