Heroku上的Django编码错误

时间:2014-04-12 01:30:37

标签: python django heroku encoding utf-8

我看到人们说使用此命令解决问题的一些话题

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)

2 个答案:

答案 0 :(得分:1)

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

我的解决方案:我发现我的heroku的蟒蛇蜡2.x。

Eveything一切正常,因为我告诉heroku使用python 3.x(作为我的开发环境)。

为此,只需在项目的根目录中添加runtime.txt文件,然后选择正确的运行时:

python-3.4.3

如下所示:https://devcenter.heroku.com/articles/python-runtimes

答案 1 :(得分:0)

我遇到了同样的问题。解决它的唯一问题是放在wsgi.py:

的开头
reload(sys)     
sys.setdefaultencoding("utf-8")

希望它有所帮助!