/ upload /上的UnicodeEncodeError

时间:2012-05-22 23:55:11

标签: python django

对于django文件上传,我在/ upload /收到一个UnicodeEncodeError,其中包含详细信息“latin-1”编解码器无法对位置中的字符进行编码。

我认为python默认编解码器是ASCII?它可以从哪个地方挑选拉丁-1?我的帖子是使用utf8。

views.py:

        kind = Kind()
        kind.style = style
        kind.kind_file.save(request.FILES['file'].name,request.FILES['file'])
        kind.save()

models.py:

def get_upload_path(instance,filename):
    return u"tmp/%s" % filename

class Kind (models.Model):
    style = models.ForeignKey(Style)
    kind_file = models.FileField(upload_to=get_upload_path)

回溯:

Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  20.                 return view_func(request, *args, **kwargs)
File "/home/vagrant/django_projects/frogtype/frogtype/font/views.py" in upload_file
  56.                 kind.kind_file.save(request.FILES['file'].name,request.FILES['file'])
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/files.py" in save
  86.         self.name = self.storage.save(name, content)
File "/usr/local/lib/python2.6/dist-packages/django/core/files/storage.py" in save
  44.         name = self.get_available_name(name)
File "/usr/local/lib/python2.6/dist-packages/django/core/files/storage.py" in get_available_name
  70.         while self.exists(name):
File "/usr/local/lib/python2.6/dist-packages/django/core/files/storage.py" in exists
  230.         return os.path.exists(self.path(name))
File "/usr/lib/python2.6/genericpath.py" in exists
  18.         st = os.stat(path)

Exception Type: UnicodeEncodeError at /upload/
Exception Value: 'latin-1' codec can't encode characters in position 87-88: ordinal not in range(256)

1 个答案:

答案 0 :(得分:0)

也许latin-1来了浏览器? (检查django中的默认编码和http标头)