我在生产服务器上遇到了 unicode 文件名的问题。当我试图保存文件 UnicodeEncodeError 异常发生。开发服务器工作正常。
www-data 的所有语言环境都是“en_US.UTF-8”。
另外,我在 /etc/apache2/envvars 中写下了
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
它没有帮助。
locale.getdefaultlocale() 正在返回 (None,None),然后我将以下行添加到 django.wsgi
os.environ['LANG']='en_US.UTF-8'
os.environ['LC_ALL']='en_US.UTF-8'
之后 locale.getdefaultlocale() 开始返回 ('en_US', 'UTF8')
我注意到 sys.getdefaultencoding() 返回 'ascii' 但我不知道如何修复它。
追溯:
Traceback:
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/contrib/admin/options.py" in wrapper
366. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/contrib/admin/sites.py" in inner
196. return view(request, *args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/transaction.py" in inner
209. return func(*args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/contrib/admin/options.py" in add_view
955. self.save_model(request, new_object, form, False)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django_pencil/admin.py" in save_model
52. super(PictureAdmin, self).save_model(request, obj, form, change)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/contrib/admin/options.py" in save_model
709. obj.save()
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/base.py" in save
463. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/myproject/myproject.com/python/lib/python2.5/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 "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/manager.py" in _insert
203. return insert_query(self.model, objs, fields, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/query.py" in insert_query
1576. return query.get_compiler(using=using).execute_sql(return_id)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
909. for sql, params in self.as_sql():
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/sql/compiler.py" in as_sql
872. for obj in self.query.objs
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/fields/files.py" in pre_save
249. file.save(file.name, file, save=False)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django_resized/forms.py" in save
31. super(ResizedImageFieldFile, self).save(name, new_content, save)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/fields/files.py" in save
86. self.name = self.storage.save(name, content)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/core/files/storage.py" in save
44. name = self.get_available_name(name)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/core/files/storage.py" in get_available_name
70. while self.exists(name):
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/core/files/storage.py" in exists
230. return os.path.exists(self.path(name))
File "/usr/lib/python2.5/posixpath.py" in exists
171. st = os.stat(path)
Exception Type: UnicodeEncodeError at /admin/django_pencil/picture/add/
Exception Value: 'ascii' codec can't encode characters in position 58-65: ordinal not in range(128)