在Django管理中保存包含非ASCII字符的ImageField时出现UnicodeEncodeError [英] UnicodeEncodeError when saving ImageField containing non-ASCII characters in Django admin
问题描述
我正在尝试以django admin内联方式上载图像文件,并尝试上载文件名中包含非ASCII字符的文件时出现UnicodeEncodeError:
I'm trying to upload an image file in django admin inlines and getting UnicodeEncodeError when trying to upload a file with a filename containing non-ascii characters:
File "/usr/local/lib/python2.6/site-packages/django/db/models/fields/files.py", line 92, in save
self.name = self.storage.save(name, content)
File "/usr/local/lib/python2.6/site-packages/django/core/files/storage.py", line 47, in save
name = self.get_available_name(name)
File "/usr/local/lib/python2.6/site-packages/django/core/files/storage.py", line 73, in get_available_name
while self.exists(name):
File "/usr/local/lib/python2.6/site-packages/django/core/files/storage.py", line 196, in exists
return os.path.exists(self.path(name))
File "/usr/local/lib/python2.6/genericpath.py", line 18, in exists
st = os.stat(path)
Django文档中有关于此问题的段落: http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#if-you-get-a-unicodeencodeerror -他们说我必须定义LANG和LC_ALL env变量,再加上使用os.env定义它们将不起作用.所以我已经在.htaccess文件中定义了它们,并且确定它们在那里:
There is a paragraph about this issue in Django docs: http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#if-you-get-a-unicodeencodeerror - they say I must define LANG and LC_ALL env variables, plus defining them using os.env won't work. So I've defined them in my .htaccess file and I'm sure they are there:
META
Variable Value
CONTENT_LENGTH '27289'
...
LANG 'en_US.UTF-8'
LC_ALL 'en_US.UTF-8'
LC_LANG 'en_US.UTF-8'
问题仍然存在. Django版本是1.2.3(最新稳定版),sys.getfilesystemencoding()(我认为与该问题有关)将返回"ANSI_X3.4-1968".
The problem still exists. Django version is 1.2.3 (latest stable), sys.getfilesystemencoding() (which I believe is relevant to the issue) returns "ANSI_X3.4-1968".
模型/管理代码没什么特别:带有ImageField的ArticleImage模型和包含ArticleImage内联的ArticleAdmin.
The model/admin code is nothing special: an ArticleImage model with ImageField, and ArticleAdmin containing ArticleImage inlines.
更新我无法解决此问题,因此我放弃了使用apache设置,并使用runfcgi + nginx启动了应用程序.上传现在可以正常工作,但是我没有将其添加为解决方案,因为这个问题是关于Apache的.
UPDATE I couldn't fix this issue so I've given up using apache setup and started the application using runfcgi + nginx. Uploads work fine now but I'm not adding this as a solution because the question was about apache.
推荐答案
在Debian(Lenny)上,您只需将以下两行添加到/etc/apache2/envvars
:
On Debian (Lenny) you simply add the following two lines to /etc/apache2/envvars
:
export LANG='en_GB.UTF-8'
export LC_ALL='en_GB.UTF-8'
...用于英国Web服务器.对于美国:
...that's for UK web servers. For US:
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
然后重新启动Apache.
And restart Apache.
这篇关于在Django管理中保存包含非ASCII字符的ImageField时出现UnicodeEncodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!