问题描述
我的问题与相似,但提出的解决方案不适合我。我试图通过Django管理应用程序上传一个名为Testaråäö.txt的文件。
我正在用Gunicorn 0.13.4和Nginx运行Django 1.3.1 0.7.6.7在Debian 6服务器上。数据库是PostgreSQL 8.4.9。其他的Unicode数据保存到数据库没有问题,所以我想这个问题一定是用文件系统。
我已经设置了
http {
charset utf-8;
}
在我的nginx.conf中。 LC_ALL和LANG被设置为'sv_SE.UTF-8'。运行语言环境验证这一点。我甚至尝试在我的nginx初始化脚本中设置LC_ALL和LANG,以确保locale设置正确。
这是回溯:
Traceback(最近一次调用的最后一个):
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/ django / core / handlers / base.py,第111行,在get_response
response = callback(request,* callback_args,** callback_kwargs)
文件/srv/.virtualenvs/letebo /lib/python2.6/site-packages/django/contrib/admin/options.py,第307行,封装
返回self.admin_site.admin_view(view)(* args,** kwargs)
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/utils/decorators.py,第93行,在_wrapped_view
response = view_func(request, * args,** kwargs)
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/views/decorators/cache.py,第79行,在_wrapped_view_func
response = view_func(request,* args,** kwargs)
文件/srv/.virtualenvs/letebo/lib /python2.6/site-packages/django/contrib/admin/sites.py,第197行,在内部
返回视图(request,* args,** kwargs)
文件/srv/django/letebo/app/cms/admin.py,第81行,在change_view
return super(PageAdmin,self).change_view(request,obj_id)
文件 /srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/utils/decorators.py,第28行,在_wrapper
返回bound_func(* args,** kwargs)
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/utils/decorators.py,第93行,在_wrapped_view
response = view_func(request,* args ,** kwargs)
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/utils/decorators.py,第24行,在bound_func
return func(self,* args2,** kwargs2)
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/transaction.py,第217行,内部
res = func(* args,** kwargs)
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/的contrib / ADM in / options.py,第985行,在change_view
self.save_formset(request,form,formset,change = True)
文件/srv/.virtualenvs/letebo/lib/ python2.6 / site-packages / django / contrib / admin / options.py,第677行,在save_formset
formset.save()
文件/srv/.virtualenvs/letebo /lib/python2.6/site-packages/django/forms/models.py,第482行保存
返回self.save_existing_objects(提交)+ self.save_new_objects(提交)
在save_new_objects
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/forms/models.py,第613行,self.new_objects.append(self.save_new(form ,commit = commit))
在save_new $ b文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/forms/models.py,第717行$ b obj.save()
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/models/base.py,第460行,在保存
self.save_base(使用=使用,force_insert = force_insert,force_update = force_update)
文件/srv/.virtualen vs / letebo / lib / python2.6 / site-packages / django / db / models / base.py,第504行,在save_base
self.save_base(cls = parent,origin = org,using = using)
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/models/base.py,第543行,在save_base
中为f在meta.local_fields如果不是isinstance(f,AutoField)]
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/models/fields/files .py,第255行,在pre_save中
file.save(file.name,file,save = False)
文件/srv/.virtualenvs/letebo/lib/python2.6 /site-packages/django/db/models/fields/files.py,第92行保存
self.name = self.storage.save(name,content)
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/core/files/storage.py,第48行,保存
name = self.get_available_name(name)
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/core/files/storage.py,第74行,在get_available_name
中,而self.exists(名称):
文件/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/core/files/storage.py,第218行,存在
return os.path.exists (self.path(name))
存在
st = os的文件/srv/.virtualenvs/letebo/lib/python2.6/genericpath.py,第18行。 stat(路径)
UnicodeEncodeError:'ascii'编解码器不能编码52-54位的字符:ordinal不在范围内(128)
更新:我试着打开调试运行的Gunicorn,并且文件上传没有任何问题。我想这意味着这个问题与Nginx有关。尽管如此,仍然打败我在哪里看。这里是来自Gunicorn和Nginx的原始响应头文件,如果它有任何意义:
Gunicorn:
<$ p $ HTTP / 1.1 302 FOUND
服务器:gunicorn / 0.13.4
日期:2012年2月9日星期四14:50:27 GMT
连接:关闭
Transfer-Encoding:chunked
过期时间:2012年2月9日(星期四)14:50:27 GMT
变化:Cookie
Last-Modified:Thu,09 Feb 2012 14:50:27 GMT
位置:http://my-server.se:8000/admin/cms/page/15/
Cache-Control:max-age = 0
Content-Type:text / html ; charset = utf-8
Set-Cookie:messages =yada yada yada; Path = /
Nginx:
<$ p $ HTTP / 1.1 500内部服务器错误
服务器:nginx / 0.7.67
日期:2012年2月9日星期四14:50:57 GMT
Content-Type :text / html; charset = utf-8
Transfer-Encoding:chunked
Connection:close
Vary:Cookie
500
pre>
看来,运行django服务的服务是apache,gunicorn + supervisor ,守护进程等...并不总是使用正确的环境变量。在这种情况下,监督员启动gunicorn,所以你必须明确告诉主管使用环境选项来使用utf-8:
$ b $
environment = LANG = en_US.UTF-8,LC_ALL = en_US.UTF-8,LC_LANG = en_US.UTF-8
My question is similar to the one reported here, but the proposed solution doesn't work for me. I'm trying to upload a file called 'Testaråäö.txt' via the Django admin app.
I'm running Django 1.3.1 with Gunicorn 0.13.4 and Nginx 0.7.6.7 on a Debian 6 server. Database is PostgreSQL 8.4.9. Other Unicode data is saved to the database with no problem, so I guess the problem must be with the filesystem somehow.
I've set
http {
charset utf-8;
}
in my nginx.conf. LC_ALL and LANG is set to 'sv_SE.UTF-8'. Running 'locale' verifies this. I even tried setting LC_ALL and LANG in my nginx init script just to make sure locale is set properly.
Here's the traceback:
Traceback (most recent call last):
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/contrib/admin/options.py", line 307, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/utils/decorators.py", line 93, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/views/decorators/cache.py", line 79, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/contrib/admin/sites.py", line 197, in inner
return view(request, *args, **kwargs)
File "/srv/django/letebo/app/cms/admin.py", line 81, in change_view
return super(PageAdmin, self).change_view(request, obj_id)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/utils/decorators.py", line 28, in _wrapper
return bound_func(*args, **kwargs)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/utils/decorators.py", line 93, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/utils/decorators.py", line 24, in bound_func
return func(self, *args2, **kwargs2)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/transaction.py", line 217, in inner
res = func(*args, **kwargs)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/contrib/admin/options.py", line 985, in change_view
self.save_formset(request, form, formset, change=True)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/contrib/admin/options.py", line 677, in save_formset
formset.save()
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/forms/models.py", line 482, in save
return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/forms/models.py", line 613, in save_new_objects
self.new_objects.append(self.save_new(form, commit=commit))
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/forms/models.py", line 717, in save_new
obj.save()
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/models/base.py", line 504, in save_base
self.save_base(cls=parent, origin=org, using=using)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/models/base.py", line 543, in save_base
for f in meta.local_fields if not isinstance(f, AutoField)]
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/models/fields/files.py", line 255, in pre_save
file.save(file.name, file, save=False)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/db/models/fields/files.py", line 92, in save
self.name = self.storage.save(name, content)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/core/files/storage.py", line 48, in save
name = self.get_available_name(name)
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/core/files/storage.py", line 74, in get_available_name
while self.exists(name):
File "/srv/.virtualenvs/letebo/lib/python2.6/site-packages/django/core/files/storage.py", line 218, in exists
return os.path.exists(self.path(name))
File "/srv/.virtualenvs/letebo/lib/python2.6/genericpath.py", line 18, in exists
st = os.stat(path)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 52-54: ordinal not in range(128)
UPDATE: I tried running Gunicorn with debugging turned on, and the file uploads without any problem at all. I suppose this must mean that the issue is with Nginx. Still beats me where to look, though. Here are the raw response headers from Gunicorn and Nginx, if it makes any sense:
Gunicorn:
HTTP/1.1 302 FOUND
Server: gunicorn/0.13.4
Date: Thu, 09 Feb 2012 14:50:27 GMT
Connection: close
Transfer-Encoding: chunked
Expires: Thu, 09 Feb 2012 14:50:27 GMT
Vary: Cookie
Last-Modified: Thu, 09 Feb 2012 14:50:27 GMT
Location: http://my-server.se:8000/admin/cms/page/15/
Cache-Control: max-age=0
Content-Type: text/html; charset=utf-8
Set-Cookie: messages="yada yada yada"; Path=/
Nginx:
HTTP/1.1 500 INTERNAL SERVER ERROR
Server: nginx/0.7.67
Date: Thu, 09 Feb 2012 14:50:57 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Cookie
500
It seems that it is a common problem that the service that runs the django service, be it apache, gunicorn + supervisor, daemontools, etc... doesn't always use the correct environment variables.
In this case, gunicorn is started by supervisor, so you have to explicitly tell supervisor to use utf-8 using the environment option:
environment=LANG=en_US.UTF-8, LC_ALL=en_US.UTF-8, LC_LANG=en_US.UTF-8
这篇关于在Django管理中上传文件时出现UnicodeEncodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!