Django在Heroku上的编码错误

Django在Heroku上的编码错误

本文介绍了Django在Heroku上的编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些话题,人们说使用这个命令来解决问题。

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'

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

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'

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

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

如何在Heroku发生这种情况?一旦我无法在Heroku上更改sitecustomize,我该如何解决?

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

错误日志

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)


推荐答案

旧问题,但我发现同样的问题。

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

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

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

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

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

要做到这一点,只需在你的项目根目录下添加一个runtime.txt文件,然后选择正确的运行时:

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

python-3.4.3

如下所示:

这篇关于Django在Heroku上的编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:33