-4
class FileUpload(models.Model):
    cstr = models.CharField(max_length=50)
    file_nm = models.CharField(max_length=250,editable=False,verbose_name='Name')
    file_type_cd = models.CharField(max_length=10,editable=False, verbose_name='Type')
    file_size_val = models.CharField(max_length=25,editable=False, verbose_name='Size in MB')
    file_upld_dts = models.DateTimeField(default =datetime.datetime.now() ,editable=False, verbose_name='Uploaded Date & Time')
    fileupload = models.FileField(upload_to=fs,verbose_name='File to Upload')
    file_title = models.CharField(max_length=250,verbose_name='Title')
    fileretention = models.IntegerField(default=18,verbose_name = '# months of Retention')
    def __unicode__(self):
        return unicode(self.file_nm)




 Traceback:
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
      111.                         response = callback(request, *callback_args, **callback_kwargs)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/contrib/admin/options.py" in wrapper
      366.                 return self.admin_site.admin_view(view)(*args, **kwargs)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
      91.                     response = view_func(request, *args, **kwargs)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
      89.         response = view_func(request, *args, **kwargs)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/contrib/admin/sites.py" in inner
      196.             return view(request, *args, **kwargs)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapper
      25.             return bound_func(*args, **kwargs)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
      91.                     response = view_func(request, *args, **kwargs)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/utils/decorators.py" in bound_func
      21.                 return func(self, *args2, **kwargs2)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/db/transaction.py" in inner
      209.                 return func(*args, **kwargs)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/contrib/admin/options.py" in add_view
      955.                 self.save_model(request, new_object, form, False)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/contrib/admin/options.py" in save_model
      709.         obj.save()
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/db/models/base.py" in save
      463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/db/models/base.py" in save_base
      551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/db/models/manager.py" in _insert
      203.         return insert_query(self.model, objs, fields, **kwargs)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/db/models/query.py" in insert_query
      1593.     return query.get_compiler(using=using).execute_sql(return_id)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
      909.         for sql, params in self.as_sql():
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in as_sql
      872.                 for obj in self.query.objs
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/db/models/fields/files.py" in pre_save
      249.             file.save(file.name, file, save=False)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/db/models/fields/files.py" in save
      86.         self.name = self.storage.save(name, content)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/core/files/storage.py" in save
      44.         name = self.get_available_name(name)
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/core/files/storage.py" in get_available_name
      70.         while self.exists(name):
    File "/opt/venv/django-cmdb/lib/python2.6/site-packages/django/core/files/storage.py" in exists
      230.         return os.path.exists(self.path(name))
    File "/usr/lib64/python2.6/genericpath.py" in exists
      18.         st = os.stat(path)

Exception Type: UnicodeEncodeError at /admin/allinone/fileupload/add/
Exception Value: 'ascii' codec can't encode character u'\u524d' in position 76: ordinal not in range(128)

这是我用于文件上传功能的 django 模型,当尝试上传具有日语名称的文件时,我遇到了错误。

我可以上传普通文件,但非英文名称的文件会出错。

我尝试设置 LANG 变量,但仍然出现此错误。

有人可以帮助解决这个问题。

4

2 回答 2

2

您需要为 Apache正确设置您的语言环境;如果没有正确的语言环境,Python 会退回到使用ASCII文件名。

LANG环境变量设置LANG=en_US.UTF-8为 Apacheenvvars文件。在 Debian 和 Ubuntu 系统上,您会发现在 中/etc/apache2/envvars,在 RedHat 上,在/etc/sysconfig/httpd.

添加行:

EXPORT LANG=en_US.utf8

如果您在查找envvars文件时遇到问题,请参阅ServerFault 上的此问题,以获取有关如何查找该文件的更多提示。

于 2013-10-21T07:27:28.203 回答
0

也许您在默认情况下使用 LANG=C 语言环境运行 Apache,这至少在使用 mod_wsgi 运行时会中断上传名称中包含特殊字符的文件。

在 /etc/apache2/envvars 中激活 UTF-8 语言环境应该可以解决问题。

在 httpd.conf 添加(或更改,如果它已经存在):

AddDefaultCharset utf-8
于 2013-10-21T08:40:59.993 回答