views:

378

answers:

3

Hello,

In my django app, I have some objects that cause the corresponding URL in the django admin to be non ascii. (for example: http://mysite/admin/myapp/myclass/Présentation/)

I can edit the object without any problem but when I save it I have the following error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 24: ordinal not in range(128), HTTP response headers must be in US-ASCII format

The strange thing is that the object is correctly saved into the database.

Does anybody know how the Django admin manages unicode? Any info, pointer or idea that can help to fix this problem would be appreciated.

Thanks in advance

Update: Here is the code of the Model

class Plugin(models.Model):
    """Some subcontent that can be added to a given page"""
    class Meta:
        ordering = ['ordering']

    name = models.CharField(max_length=32, primary_key=True)
    div_id = models.CharField(default='rightcol', max_length=32)
    published = models.BooleanField(default=True,
        help_text=_("If this is not checked, it is not displayed on the page."))
    ordering = models.IntegerField(default=1,
        help_text=_("plugins are sorted with this number in ascending order"))
    content = models.TextField(blank=True)
    registration_required = models.BooleanField(_('registration required'),
        help_text=_("If this is checked, only logged-in users will be able to view the page."))

    def __unicode__(self):
        return u"%s -- %s" % (self.name, self.div_id)

Update: That's clear that non-ascii character are not recommended in an URL. That's the cause of my problem and I have changed that.

Does anybody know what is used by the Django admin to build the URL of an object. I guess that it is the primary key. Is it right? is there a way to force Django to use something else and to retrieve the object safely?

+1  A: 

I'm pretty sure your database is probably using the latin1 encoding. Django supposes you have everything set as unicode (utf8).

To check this out, get into MySQL Shell and type:

mysql> show variables like 'char%';

If you see a bunch of latin1 (or any other encoding that isn't utf8, except binary), you'll have to do this: Open up my.cnf and look for the [mysqld] section. Make sure after tmpdir = /tmp, you have the following lines:

default-character-set=utf8
collation_server=utf8_unicode_ci
character_set_server=utf8
skip-external-locking
skip-character-set-client-handshake

Restart the server.

You'll have to recreate or manually edit the encoding of all databases and table you have, changing my.cnf only affects the databases that will be created.

Hope I've helped.

Edit: By the way, on which Django version are you on? Seems like this was a bug that was fixed on 1.1: http://code.djangoproject.com/ticket/10267

Clash
I will check that with my host provider. I have | character_set_client | latin1 || character_set_connection | latin1 || character_set_database | utf8 || character_set_filesystem | binary || character_set_results | latin1 || character_set_server | latin1 |
luc
If they don't change it, you can just recreate your database, setting everything to utf8
Clash
According to my host provider, this pb is not related to db encoding. I think he's right because this pb is sporadic. Thanks for your help
luc
I think that this is not database related. Data is saved correctly. The problem comes from french letters in URL. Some parts of the django code seems to accept it and some not
luc
Have you checked this link? http://code.djangoproject.com/ticket/10267Seems like there was a bug that was fixed on Django 1.1, what version are you on?
Clash
A: 

in your model, have you tried putting

class Model(models.Model):
   def __unicode__(self):
       return self.name ## having a model object named "name"

Not sure if this is the answer you are searhing for, but have you tried?

PirosB3
My model has a unicode. Django doesn't use the __unicode__ for building the url. I think it will not be able to safely retrieve the object. I guess that it uses the primary key. Thanks for your help
luc
A: 

I am now using the default id as primary key for every class of my model. As a consequence, I don't have forbidden characters in the url when accessing objects from the admin site.

I do recommend to keep the default id as primary key in most cases

luc