日文名称的文件引发错误

时间:2013-10-21 07:03:51

标签: python django django-models django-templates

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变量,但我仍然遇到此错误。

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

2 个答案:

答案 0 :(得分:2)

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

LANG环境变量设置为LANG=en_US.UTF-8到Apache envvars文件。在Debian和Ubuntu系统上,您可以在/etc/apache2/envvars,RedHat,/etc/sysconfig/httpd中找到它。

添加以下行:

EXPORT LANG=en_US.utf8

如果您无法找到envvars文件,请参阅ServerFault上的this question以获取有关如何查找该文件的更多提示。

答案 1 :(得分:0)

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

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

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

AddDefaultCharset utf-8