我只在生产服务器上遇到此问题。当我尝试上传带有俄语符号命名的文件时,我收到错误
UnicodeEncodeError: 'ascii' codec can't encode characters in position...
保存:
#... code above ...
file = request.FILES.get('file')
try:
item = ModelItem(
model=file,
title=file.name,
)
item.save()
# ... code below ...
完整追溯:
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 589, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 617, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 698, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 731, in _do_insert
using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 921, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 919, in execute_sql
for sql, params in self.as_sql():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 877, in as_sql
for obj in self.query.objs
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py", line 301, in pre_save
file.save(file.name, file, save=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py", line 89, in save
self.name = self.storage.save(name, content)
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py", line 50, in save
name = self.get_available_name(name)
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py", line 75, in get_available_name
while self.exists(name):
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py", line 265, in exists
return os.path.exists(self.path(name))
File "/usr/lib/python2.7/genericpath.py", line 18, in exists
os.stat(path)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-34: ordinal not in range(128)
我试图设置
os.environ['LANG'] = 'en_US.UTF-8'
当服务器启动时,我也检查了
的输出locale -a
并找到所有需要的语言环境,但它没有帮助。