Code

Opened 3 years ago

Closed 3 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:

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 3 years ago.

Download all attachments as: .zip

Change History (8)

Changed 3 years ago by panosl

comment:1 Changed 3 years ago by panosl

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 follow-up: Changed 3 years ago by rasca

  • Resolution set to worksforme
  • Status changed from new to closed

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

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

  • Resolution worksforme deleted
  • Status changed from closed to reopened

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 3 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 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 Changed 3 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 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 Changed 3 years ago by rasca

  • Needs tests set
  • Triage Stage changed from Unreviewed to Design 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 3 years ago by ramiro

  • Resolution set to invalid
  • Status changed from reopened to closed

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)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.