编码错误在Django在Heroku(Encoding error in Django on Her

2019-10-19 20:13发布

我看到一些主题里的人说,使用这个命令来解决问题

heroku config:add LANG=en_US.UTF-8

我做到了,但已经不断得到编码的问题,像这样的

UnicodeEncodeError at /admin/controladoria/orgao/2/

'ascii' codec can't encode character u'\xe7' in position 23: ordinal not in range(128)

如果我尝试运行上面的命令是我得到了什么....

daniel@daniel-VirtualBox: (dev *) $ heroku config:add LANG=en_US.UTF-8
Setting config vars and restarting rocky-hollows-8072... done, v11
LANG: en_US.UTF-8
daniel@daniel-VirtualBox: (dev *) $ heroku run bash
Running `bash` attached to terminal... up, run.7893
~ $ python
Python 2.7.6 (default, Jan 16 2014, 02:39:37) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'

在我的机器上,用sitecustomize.py编辑我有

Python 2.7.5 (default, Mar 11 2014, 21:42:34) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'

这是应该发生的? 我知道,如果我把sitecustomize.py默认的编码,我没有问题。

如何是,应该在Heroku上出现呢? 我该如何解决,一旦我不能在Heroku上改变sitecustomize?

错误日志

Traceback:
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  432.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  198.             return view(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  25.                 return func(self, *args2, **kwargs2)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/transaction.py" in inner
  339.                 return func(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/options.py" in change_view
  1233.                 self.log_change(request, new_object, change_message)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/options.py" in log_change
  614.             object_repr=force_text(object),
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
  108.                     s = six.text_type(bytes(s), encoding, errors)

Exception Type: UnicodeEncodeError at /admin/controladoria/orgao/2/
Exception Value: 'ascii' codec can't encode character u'\xe7' in position 23: ordinal not in range(128)

Answer 1:

老问题,但我发现了同样的问题。

我的解决方案:我发现我的Heroku的蟒蜡2.x版本

Eveything就OK,因为我告诉Heroku的使用Python 3.x的(如我的开发环境)。

要做到这一点,只是在你的项目的根添加runtime.txt文件,并选择合适的运行时间:

python-3.4.3

由于这里看到: https://devcenter.heroku.com/articles/python-runtimes



Answer 2:

我有同样的问题。 它解决了我唯一的办法就是放wsgi.py的开头:

reload(sys)     
sys.setdefaultencoding("utf-8")

希望它可以帮助别人!



文章来源: Encoding error in Django on Heroku