Encoding error in Django on Heroku

老子叫甜甜 提交于 2020-01-05 17:30:34

问题


I see some topics where people say to use this command to solve the problem

heroku config:add LANG=en_US.UTF-8

I did it already but keep getting encoding problem, like this

UnicodeEncodeError at /admin/controladoria/orgao/2/

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

if I try to run the command above it is what I got....

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'

at my machine, with sitecustomize.py edited i got

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'

It is supposed to happen? I know that if I put in sitecustomize.py the default encoding I don't have issues.

How is that supposed to happen on Heroku? How can I solve that once I can't change sitecustomize on Heroku?

Error log

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)

回答1:


Old question, but I've found the same issue.

My solution: I've found that my heroku's python wax 2.x.

Eveything went OK as I told heroku to use python 3.x (as my dev environment).

To do that, just add a runtime.txt file at the root of you project, and choose the right runtime:

python-3.4.3

As seen here: https://devcenter.heroku.com/articles/python-runtimes




回答2:


I had the same problem. The only thing that solved it to me was put in the beginning of wsgi.py:

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

Hope it helps someone!



来源:https://stackoverflow.com/questions/23025001/encoding-error-in-django-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!