'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)

1,332 views
Skip to first unread message

Alexander Bolotnov

unread,
Feb 22, 2011, 3:37:44 PM2/22/11
to django-d...@googlegroups.com
I created a simple model:

class Menu(models.Model):
    name = models.CharField(null=False, blank=False,unique=True,max_length=50)
    url = models.CharField(null=False, blank=False, unique=True,max_length=100)
    sortOrder = models.IntegerField(null=False, blank=False, default=0)
    def __unicode__(self):
        return 'name: {0}, url: {1}'.format(self.name, self.url)

and hooked it to admin (all running on windows 7, django dev server)

now when I add a new item via admin and the name is in Russian (Like "коллекция картинок") it throws the following exception. How do I fix this?

UnicodeEncodeError at /admin/picviewer/menu/add/

'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)
Request Method:POST
Request URL:http://127.0.0.1:8000/admin/picviewer/menu/add/
Django Version:1.2.5
Exception Type:UnicodeEncodeError
Exception Value:
'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)
Exception Location:D:\~Sasha\www.zavalen.ru\django\zavalen\picviewer\models.py in __unicode__, line 43
Python Executable:D:\install\python27\python.exe
Python Version:2.7.1
Python Path:['D:\\~Sasha\\www.zavalen.ru\\django\\zavalen', 'C:\\Windows\\system32\\python27.zip', 'D:\\install\\python27\\DLLs', 'D:\\install\\python27\\lib', 'D:\\install\\python27\\lib\\plat-win', 'D:\\install\\python27\\lib\\lib-tk', 'D:\\install\\python27', 'D:\\install\\python27\\lib\\site-packages']
Server time:Tue, 22 Feb 2011 23:24:58 +0300

Alexander Bolotnov

unread,
Feb 22, 2011, 3:41:33 PM2/22/11
to django-d...@googlegroups.com
I fixed this by change the unicode to return self.name - but why was this failing anyway?

rikuth...@gmail.com

unread,
Feb 22, 2011, 3:53:59 PM2/22/11
to django-d...@googlegroups.com, Alexander Bolotnov
The problem is, I think, that you're trying to insert a non-ascii string, inside an ascii string:

def __unicode__(self):
        return 'name: {0}, url: {1}'.format(self.name, self.url)

Probably, self.name is an unicode string, and you're trying to put it on a non-unicode string. Try this:

def __unicode__(self):
        return u'name: {0}, url: {1}'.format(self.name, self.url)

It may work. If it doesn't look for "smart_unicode" and "force_unicode" Django functions.

Regards

2011/2/22 Alexander Bolotnov <abol...@gmail.com>
I fixed this by change the unicode to return self.name - but why was this failing anyway?

--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.



--
Pablo Recio Quijano

Desarrollador Django
Yaco Sistemas - http://www.yaco.es/

Ian Kelly

unread,
Feb 22, 2011, 3:54:34 PM2/22/11
to django-d...@googlegroups.com
2011/2/22 Alexander Bolotnov <abol...@gmail.com>

I created a simple model:

class Menu(models.Model):
    name = models.CharField(null=False, blank=False,unique=True,max_length=50)
    url = models.CharField(null=False, blank=False, unique=True,max_length=100)
    sortOrder = models.IntegerField(null=False, blank=False, default=0)
    def __unicode__(self):
        return 'name: {0}, url: {1}'.format(self.name, self.url)

and hooked it to admin (all running on windows 7, django dev server)

now when I add a new item via admin and the name is in Russian (Like "коллекция картинок") it throws the following exception. How do I fix this?

Please direct questions about how to use Django to the django-users mailing list.  This list is for development of Django itself.

Thanks,
Ian
Reply all
Reply to author
Forward
0 new messages