0

对于 django 文件上传,我在 /upload/ 处收到一个 UnicodeEncodeError,详细信息为“latin-1'编解码器无法在位置编码字符”。

我以为python默认编解码器是ASCII?它可以从哪里挑选 latin-1?我的 postgres 使用的是 utf8。

视图.py:

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

模型.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)
4

1 回答 1

0

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

于 2012-05-23T00:22:37.243 回答