John
John

Reputation: 4261

Exception UnicodeEncodeError while uploading an image

In the Django application, user is uploading an image using a form. I am not sure why. I am able to upload files with accented characters in name. The exception is below

File "pathto/python2.7/django/core/handlers/base.py", line 111, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/pathto/python2.7/django/contrib/auth/decorators.py", line 23, in _wrapped_view
   return view_func(request, *args, **kwargs)

 File "/pathto/views.py", line 75, in upload_image
   obj = form.save(request.user)

 File "/form.py", line 88, in save
   obj.save()

 File "/pahtto/python2.7/django/db/models/base.py", line 460, in save
   self.save_base(using=using, force_insert=force_insert, force_update=force_update)

 File "/pathto/python2.7/django/db/models/base.py", line 543, in save_base
   for f in meta.local_fields if not isinstance(f, AutoField)]

 File "/pathto/lib/python2.7/django/db/models/fields/files.py", line 255, in pre_save
   file.save(file.name, file, save=False)

 File "/pathto/extrantool.py", line 96, in save
   super(ImageWithThumbsFieldFile, self).save(name, content, save)

 File "/pathto/python2.7/django/db/models/fields/files.py", line 92, in save
   self.name = self.storage.save(name, content)

 File "/pathto/python2.7/django/core/files/storage.py", line 48, in save
   name = self.get_available_name(name)

 File "/pathto/python2.7/django/core/files/storage.py", line 74, in get_available_name
   while self.exists(name):

 File "/pathto/python2.7/django/core/files/storage.py", line 218, in exists
   return os.path.exists(self.path(name))

 File "/usr/local/lib/python2.7/genericpath.py", line 18, in exists
   os.stat(path)

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 66: ordinal not in range(128)

Upvotes: 1

Views: 2161

Answers (1)

Richard Huxton
Richard Huxton

Reputation: 22893

At a guess, Brandon is right and there is a non-ascii character in the filename. Googling your error message

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 66

gives me the following as the first two links:

  1. Python Unicode Error blog entry
  2. Python official Unicode docs

Adding "django" to the search actually gives us a StackOverflow answer!

UnicodeEncodeError: 'ascii' codec can't encode character

So - check your language settings for django would be my suggestion.

Upvotes: 3

Related Questions