上传图像时出现UnicodeEncodeError异常

时间:2012-05-03 17:42:14

标签: django postgresql

在Django应用程序中,用户正在使用表单上传图像。我不知道为什么。我可以上传名字中带重音字符的文件。例外情况如下

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)

1 个答案:

答案 0 :(得分:2)

猜测,Brandon是对的,文件名中有一个非ascii字符。谷歌搜索您的错误消息

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

给出了以下两个链接:

  1. Python Unicode Error blog entry
  2. Python official Unicode docs
  3. 在搜索中添加“django”实际上为我们提供了StackOverflow答案!

    UnicodeEncodeError: 'ascii' codec can't encode character

    所以 - 检查django的语言设置是我的建议。