3

運用サーバーでの Unicode ファイル名に問題があります。ファイルを保存しようとすると、UnicodeEncodeError 例外が発生します。Devserver は正常に動作します。

www-data のすべてのロケールは「en_US.UTF-8」です。

また、 /etc/apache2/envvars に書き留めました

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

それは役に立ちませんでした。

locale.getdefaultlocale() が (None,None) を返していたので、次の行を 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)
4

2 に答える 2

5

django.wsgi にも追加します。

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

単純な sys.setdefaultencoding('utf-8') は役に立ちません。リロードが重要です これは Python 2.x の問題であり、django の問題ではありません

于 2012-10-06T16:46:40.170 に答える
0

Debianは「/etc/ apache2/envvars」を使用するとは思わない。Pythonスクリプト内からこれらの値を設定すると、プロセスの起動時にのみ発生するいくつかのことが初期化されないため、機能しません。

envvarsが参照されない場合は、Apacheのinit.d起動スクリプトで変数を設定するか、mod_wsgi 3.4を使用してデーモンモードを使用し、「lang」および「locale」オプションをWSGIDaemonProcessディレクティブに設定する必要があります。

于 2012-10-07T12:08:48.393 に答える