UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)

2,678 views
Skip to first unread message

Vladimir

unread,
Mar 3, 2011, 1:48:21 AM3/3/11
to Django users
Good morning!
In me first exercise project with Django I met an error message:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position
0: ordinal not in range(128). Admin pages are built without css.
Then I changed all .py-files entering
# -*- coding: utf-8 -*-
into the first row and recreated database:
ALTER DATABASE `djangodb` DEFAULT CHARACTER SET utf-8 COLLATE
utf8_unicode_ci
I also tryed COLLATE utf8_swedish_ci. Then I recreated tables with
manage.py syncdb.
No effect. Is this a typical problem of Django beginners?
I need only russian and english languages in my projects endeed.

Szabo, Patrick (LNG-VIE)

unread,
Mar 3, 2011, 2:04:25 AM3/3/11
to django...@googlegroups.com
Good morning !

Unfortunately i cant help you with your problem because i have the same problem here ;)
I can however answer youre question if this is a common problem for django beginners.
I don't think it's common for django beginners, i'd rather say it's common for python beginners - like me ;)

Kind regards


. . . . . . . . . . . . . . . . . . . . . . . . . .
Patrick Szabo
XSLT Developer
LexisNexis
Marxergasse 25, 1030 Wien

mailto:patric...@lexisnexis.at
Tel.: +43 (1) 534 52 - 1573
Fax: +43 (1) 534 52 - 146


-----Ursprüngliche Nachricht-----

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

Daniel Roseman

unread,
Mar 3, 2011, 2:46:49 AM3/3/11
to django...@googlegroups.com
You haven't posted any code, so it's impossible to help you. The error message will have included a traceback, which you should post here, along with the relevant model or view.

In my experience, though, this error is usually caused by returning non-unicode strings from the __unicode__ method of a model.
--
DR. 

Vladimir

unread,
Mar 3, 2011, 4:01:22 AM3/3/11
to Django users
1. web browser does not give messages, in command line I see:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py",
line 280, in run
self.result=application(self.environ, self.start_response)
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py",
line 709, in __call__
mime_type=mimetypes.guess_type(file_path)[0]
File "C:\Python27\lib\mimetypes.py", line 294, in guess_type
init()
File "C:\Python27\lib\mimetypes.py", line 355, in init
db.read_windows_registry()
File "C:\Python27\lib\mimetypes.py", line 259, in
read_windows_registry
for ctype in enum_types(mimedb):
File "C:\Python27\lib\mimetypes.py", line 249, in enum_types
ctype=ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position
0: ordinal not in range(128)

2. mysite\blog\models.py:
# -*- coding: utf-8 -*-

from django.db import models
from django.contrib import admin

class BlogPost(models.Model):
title = models.CharField(max_length=150)
body = models.TextField()
timestamp = models.DateTimeField()

class Meta:
ordering = ('-timestamp',)

class BlogPostAdmin(admin.ModelAdmin):
list_display = ('title', 'timestamp')

Szabo, Patrick (LNG-VIE)

unread,
Mar 3, 2011, 4:10:53 AM3/3/11
to django...@googlegroups.com
If you want to see errors in the browser you might want to set "DEBUG = True" in your settings.py

Kind regards


. . . . . . . . . . . . . . . . . . . . . . . . . .
Patrick Szabo
XSLT Developer
LexisNexis
Marxergasse 25, 1030 Wien


-----Ursprüngliche Nachricht-----

Von: django...@googlegroups.com [mailto:django...@googlegroups.com] Im Auftrag von Vladimir

--

Vladimir

unread,
Mar 3, 2011, 4:56:22 AM3/3/11
to Django users


On 3 мар, 12:10, "Szabo, Patrick \(LNG-VIE\)"
<patrick.sz...@lexisnexis.at> wrote:
> If you want to see errors in the browser you might want to set "DEBUG = True" in your settings.py
>
> Kind regards
>
> . . . . . . . . . . . . . . . . . . . . . . . . . .
> Patrick Szabo
>  XSLT Developer
> LexisNexis
> Marxergasse 25, 1030 Wien
>
> mailto:patrick.sz...@lexisnexis.at

Vladimir

unread,
Mar 3, 2011, 4:59:55 AM3/3/11
to Django users
Thank You very much!
It is set by default but there is no error message. Some days ago,
when I had another problem, web browser showed messages.


On 3 мар, 12:10, "Szabo, Patrick \(LNG-VIE\)"
<patrick.sz...@lexisnexis.at> wrote:
> If you want to see errors in the browser you might want to set "DEBUG = True" in your settings.py
>
> Kind regards
>
> . . . . . . . . . . . . . . . . . . . . . . . . . .
> Patrick Szabo
>  XSLT Developer
> LexisNexis
> Marxergasse 25, 1030 Wien
>
> mailto:patrick.sz...@lexisnexis.at

Karen Tracey

unread,
Mar 3, 2011, 8:24:39 AM3/3/11
to django...@googlegroups.com
On Thu, Mar 3, 2011 at 4:01 AM, Vladimir <vev...@yandex.ru> wrote:
1. web browser does not give messages, in command line I see:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py",
line 280, in run
   self.result=application(self.environ, self.start_response)
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py",
line 709, in __call__
   mime_type=mimetypes.guess_type(file_path)[0]
File "C:\Python27\lib\mimetypes.py", line 294, in guess_type
   init()
File "C:\Python27\lib\mimetypes.py", line 355, in init
   db.read_windows_registry()
File "C:\Python27\lib\mimetypes.py", line 259, in
read_windows_registry
   for ctype in enum_types(mimedb):
File "C:\Python27\lib\mimetypes.py", line 249, in  enum_types
   ctype=ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position
0: ordinal not in range(128)

This looks like a bug in Python. Traceback is showing Python mimetypes init() trying to read the windows registry and running into trouble. The code in question isn't even using any parameters passed in from the Django code calling it, so it's unlikely any change to Django (other than not using mimetypes guess_type) would help to fix it.

This has come up before on this list, and a workaround was mentioned here:

http://groups.google.com/group/django-users/msg/7fe07998db471693

A real fix I believe would need to be made to the Python library code to ensure that it doesn't die if the windows registry contains non-ASCII data.

Karen
--
http://tracey.org/kmt/

Perses Titan

unread,
Mar 3, 2011, 8:57:46 AM3/3/11
to Django users
Hello!

I got the same error when i tried to work with Cyrillic charset in
Python.
In my case it was pure Python problem.
Here is good explanation why it happened: http://docs.python.org/howto/unicode.html
and http://python.su/forum/viewtopic.php?pid=6476 (rus)

In your case, most probably django crashs when it reads nonASCII
characters from DB.


Wadim.

Karen Tracey

unread,
Mar 3, 2011, 9:07:14 AM3/3/11
to django...@googlegroups.com, Perses Titan
On Thu, Mar 3, 2011 at 8:57 AM, Perses Titan <pers...@gmail.com> wrote:
In your case, most probably django crashs when it reads nonASCII
characters from DB.


No. The posted traceback clearly shows where the error is occurring and it is nowhere in Django. As I posted earlier it looks like a bug in the Python library. I'd also guess it is new with Python 2.7 since I cannot recall seeing this problem posted before Python 2.7 came out. The right place to pursue this particular problem is in the Python bug tracker -- for all I know it has already been noted and fixed, I have not attempted to track it down from that side.

Django fully supports non-ASCII data in the database, in template files, dynamically generated by application code. (Your application, of course, must ALSO be coded to properly handled non-ASCII data.)

Posting that Django probably crashed when reading non-ASCII data from the database is misleading and in this case where the traceback was posted flat-out wrong.

Karen
--
http://tracey.org/kmt/

Vladimir

unread,
Mar 3, 2011, 12:16:21 PM3/3/11
to Django users
Dear friends!
Karen Tracey gave me great reference:
http://www.google.com/url?sa=D&q=http://groups.google.com/group/django-users/msg/7fe07998db471693
There is reference to original work:
http://victor-k-development.blogspot.com/2010/07/unicodedecodeerror-django.html
Victor discovered a cause of this bug: python 2.7 works erroneously in
case of non-latin names in Windows register. Windows often do so for
russian users.
I was frightened to edit Windows register, so I deleted Python 2.7 and
installed Python 2.6.
This helped me.
Thank You all very much!
Could you organisers place this information to the Django site for all
russian beginners ?

Karen Tracey

unread,
Mar 5, 2011, 12:06:28 AM3/5/11
to django...@googlegroups.com
On Thu, Mar 3, 2011 at 8:24 AM, Karen Tracey <kmtr...@gmail.com> wrote:
A real fix I believe would need to be made to the Python library code to ensure that it doesn't die if the windows registry contains non-ASCII data.

In fact there is a bug open in the Python bug tracker for this:

http://bugs.python.org/issue9291

Karen
--
http://tracey.org/kmt/

Christophe

unread,
Mar 9, 2012, 10:24:03 AM3/9/12
to django...@googlegroups.com
FYI I get the same problem on Ubuntu 10.04 LTS:


Traceback (most recent call last):
  File "server/manage.py", line 11, in <module>
    execute_manager(settings)
  File "/home/user/.ve_config/myproject/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/home/user/.ve_config/myproject/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/.ve_config/myproject/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/user/.ve_config/myproject/lib/python2.7/site-packages/django/core/management/base.py", line 219, in execute
    self.validate()
  File "/home/user/.ve_config/myproject/lib/python2.7/site-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/home/user/.ve_config/myproject/lib/python2.7/site-packages/django/core/management/validation.py", line 102, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
  File "/home/user/.ve_config/myproject/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
    db_version = self.connection.get_server_version()
  File "/home/user/.ve_config/myproject/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 314, in get_server_version
    m = server_version_re.match(self.connection.get_server_info())
  File "/opt/google_appengine/google/storage/speckle/python/api/rdbms.py", line 733, in get_server_info
    response = self.MakeRequest('GetMetadata', request)
  File "/opt/google_appengine/google/storage/speckle/python/api/rdbms.py", line 604, in MakeRequest
    response = self._MakeRetriableRequest(stub_method, request)
  File "/opt/google_appengine/google/storage/speckle/python/api/rdbms.py", line 628, in _MakeRetriableRequest
    response = self.MakeRequestImpl(stub_method, request)
  File "/opt/google_appengine/google/storage/speckle/python/api/rdbms_googleapi.py", line 210, in MakeRequestImpl
    response = getattr(self._client, stub_method)(request)
  File "/opt/google_appengine/google/storage/speckle/python/api/rdbms_googleapi.py", line 135, in GetMetadata
    'jdbc/getMetadata', request, sql_pb2.MetadataResponse)
  File "/opt/google_appengine/google/storage/speckle/python/api/rdbms_googleapi.py", line 157, in _MakeRequest
    return request.execute()
  File "/opt/google_appengine/lib/google-api-python-client/apiclient/http.py", line 86, in execute
    headers=self.headers)
  File "/opt/google_appengine/lib/google-api-python-client/oauth2client/client.py", line 483, in new_request
    redirections, connection_type)
  File "/opt/google_appengine/lib/httplib2/httplib2/__init__.py", line 1445, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/opt/google_appengine/lib/httplib2/httplib2/__init__.py", line 1197, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/opt/google_appengine/lib/httplib2/httplib2/__init__.py", line 1134, in _conn_request
    conn.request(method, request_uri, body, headers)
  File "/opt/lib/python2.7/httplib.py", line 946, in request
    self._send_request(method, url, body, headers)
  File "/opt/lib/python2.7/httplib.py", line 987, in _send_request
    self.endheaders(body)
  File "/opt/lib/python2.7/httplib.py", line 940, in endheaders
    self._send_output(message_body)
  File "/opt/lib/python2.7/httplib.py", line 801, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf8 in position 26: ordinal not in range(128)

This happens when I "manage.py syncdb" to the Google SQL Cloud.
Reply all
Reply to author
Forward
0 new messages