2012-10-06 77 views
3

我在生產服務器上的unicode文件名有問題。當我試圖保存UnicodeEncodeError異常發生的文件時。 Devserver工作正常。Django UnicodeEncodeError,Apache2與Debian lenny上的mod_wsgi

爲WWW的數據的所有區域設置爲 '的en_US.UTF-8'。

而且,我寫下的/ etc/apache2的/ envvars中

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

它並沒有幫助。

locale.getdefaultlocale()返航(無,無),然後,添加以下行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) 

回答

5

也增加django.wsgi:

import sys 
reload(sys) 
sys.setdefaultencoding('utf-8') 

簡單sys.setdefaultencoding函數( 'UTF-8')不會幫助,重載是重要 這是蟒蛇2 .X問題,而不是Django的

+1

現在,sys.getdefaultencoding()返回'utf-8',但UnicodeEncodeError仍然會發生。需要回溯 – un1t

+0

。 – RaSergiy

+0

我添加了回溯到主題。 – un1t

0

Debian不會相信用 '的/ etc/apache2的/ envvars中'。從Python腳本內部設置這些值將不起作用,因爲它不會初始化僅在進程啓動時發生的一些事情。

如果未查閱envvars,或者使用mod_wsgi 3.4並使用守護進程模式,將'lang'和'locale'選項設置爲WSGIDaemonProcess指令,則需要在Apache的init.d啓動腳本中設置變量。

+0

我用daemon模式,但似乎mod_wsgi的太舊 (II中的libapache2-MOD-WSGI 2.5-1〜lenny1)還有我做檢查/etc/init.d/apache2文件 - 它是二進制的。 – un1t