Opened 14 years ago

Closed 14 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: no UI/UX: no

Description

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 admin.py 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/options.py, 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 14 years ago.

Download all attachments as: .zip

Change History (8)

by panosl, 14 years ago

Attachment: options.patch added

comment:1 by panosl, 14 years ago

Has patch: set

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

comment:2 by rasca, 14 years ago

Resolution: worksforme
Status: newclosed

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

in reply to:  2 comment:3 by panosl, 14 years ago

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 by rasca, 14 years ago

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 models.py,
then added admin.site.register(MyModel) to admin.py,
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 by panosl, 14 years ago

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 http://panos.solhost.org/temp/uerror_project.tar.gz

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 by rasca, 14 years ago

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 by Ramiro Morales, 14 years ago

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 (http://docs.djangoproject.com/en/1.2/topics/i18n/internationalization/#lazy-translation)

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