gpt4 book ai didi

Django UnicodeEncodeError,在 Debian lenny 上带有 mod_wsgi 的 Apache2

转载 作者:行者123 更新时间:2023-12-01 11:49:32 26 4
gpt4 key购买 nike

我在生产服务器上遇到 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)

最佳答案

同时添加到django.wsgi:

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

简单的 sys.setdefaultencoding('utf-8') 无济于事,重新加载很重要这是 python 2.x 的问题,不是 django

关于Django UnicodeEncodeError,在 Debian lenny 上带有 mod_wsgi 的 Apache2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12761139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com