在 Django admin 中保存包含非 ASCII 字符的 ImageField 时出现 UnicodeEncodeError

发布于 2024-10-06 16:05:35 字数 1576 浏览 0 评论 0原文

我正在尝试在 django admin inlines 中上传图像文件,并在尝试上传文件名包含非 ascii 字符的文件时收到 UnicodeEncodeError:

 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 文件中定义了它们,并且我确信它们在那里:

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”。

模型/管理代码没有什么特别的:带有 ImageField 的 ArticleImage 模型,以及包含 ArticleImage 内联的 ArticleAdmin。

更新 我无法解决这个问题,所以我放弃使用 apache 设置并使用 runfcgi + nginx 启动应用程序。上传现在工作正常,但我没有将其添加为解决方案,因为问题是关于 apache 的。

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)

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'

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".

The model/admin code is nothing special: an ArticleImage model with ImageField, and ArticleAdmin containing ArticleImage inlines.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春如此纠结 2024-10-13 16:05:35

在 Debian (Lenny) 上,您只需将以下两行添加到 /etc/apache2/envvars 中:

export LANG='en_GB.UTF-8'
export LC_ALL='en_GB.UTF-8'

...这适用于英国 Web 服务器。对于美国:

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

并重新启动 Apache。

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'

...that's for UK web servers. For US:

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

And restart Apache.

墟烟 2024-10-13 16:05:35

您应该尝试为整个 Apache 2 环境定义 LANG 和 LC_ALL。

对于我的部署,我还确保 python 默认系统编码也设置为 utf-8。

对于我通常创建/编辑 sitecustomize.py 的 Python 默认编码,请参阅 http://blog.ianbicking.org/illusive-setdefaultencoding .html

至于 Apache - 初始化脚本 /etc/init.d/apache2 (Ubuntu 8.04 LTS) 中有一行创建了环境。我在那里添加了正确的 LC_ALL、LANG。基本上它应该位于所有操作系统的服务器初始化脚本中。

You should try defining the LANG and LC_ALL for the whole Apache 2 environment.

For my deployments I also make sure that the python default system encoding is set to utf-8 as well.

For the Python default encoding I usually create/edit sitecustomize.py, see http://blog.ianbicking.org/illusive-setdefaultencoding.html

As for Apache - there is line in init script /etc/init.d/apache2 (Ubuntu 8.04 LTS) that creates the environment. I added the correct LC_ALL, LANG there. Basically it should be in the server init scripts somewhere for all the OSes.

以酷 2024-10-13 16:05:35

你可以这样做。

在linux中:

echo $LANG   i got zh_CN.UTF-8

在apache2/envvars

export LANG='zh_CN.UTF-8'  #keep this variable like echo $LANG.
export LC_ALL='zh_CN.UTF-8' #the same.

https ://docs.djangoproject.com/en/1.4/howto/deployment/modpython/#if-you-get-a-unicodeencodeerror
这个医生也帮助我。

我认为这是操作系统和apache的问题!

your can do like this.

in linux:

echo $LANG   i got zh_CN.UTF-8

in apache2/envvars

export LANG='zh_CN.UTF-8'  #keep this variable like echo $LANG.
export LC_ALL='zh_CN.UTF-8' #the same.

https://docs.djangoproject.com/en/1.4/howto/deployment/modpython/#if-you-get-a-unicodeencodeerror
this doc is help me too.

i think it's the os and apache problem!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文