DjangoUnicodeDecode错误:'utf8'编解码器无法解码位置51的字节0x80



我将一个查询id对象传递给一个视图,然后该视图获取该对象,然后调用以下函数:

def portAdmin(self,status):
    status = status
    self.adminStateDict = {
                        'activate':      tuple([tuple([1,3,6,1,2,1,2,2,1,7,self.snmpPortOID]),rfc1902.Integer32(1)]),
                        'deactivate' : tuple([tuple([1,3,6,1,2,1,2,2,1,7,self.snmpPortOID]),rfc1902.Integer32(2)]),
                        }
    (errorIn, activateErrorStatus, errorIndex, varBinds) = cmdgen.CommandGenerator().setCmd(
                                                cmdgen.CommunityData('my-agent', '.xxxx', 0),
                                                cmdgen.UdpTransportTarget((self.snmpIp, 161)),
                                                self.adminStateDict[status]
                                                )

但是在没有从函数返回的情况下,当我请求页面时,我得到了这个错误:

    Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 283, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
    return self.application(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 272, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 169, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 59, in technical_500_response
    html = reporter.get_traceback_html()
  File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 117, in get_traceback_html
    frame['vars'] = [(k, force_escape(pprint(v))) for k, v in frame['vars']]
  File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 34, in _dec
    args[0] = force_unicode(args[0])
  File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 93, in force_unicode
    raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 51: invalid start byte. You passed in 'MibTableColumn((1, 3, 6, 1, 6, 3, 18, 1, 1, 1, 4), x80x00Oxb8x05xc0xa8x06 x0cr)' (<type 'str'>)

但是,当我在django shell中调用相同的函数时,这可以很好地工作。我被难住了。我想知道的是为什么:1.它在shell中工作,而不是在web服务器上。2.如何使用Django/WSGI使其在web服务器上工作。

谢谢。

因为Apache的系统默认本地通常是ASCII,而在您的用户帐户中是UTF-8。

您可以修复代码,使其不依赖于隐式强制,这将使用进程的系统默认编码,也可以覆盖Apacheinit环境,将LANG设置为UTF-8变体。

尝试在谷歌上搜索"Apache UTF-8语言环境",并为您的Apache发行版找到合适的方法。

相关内容

最新更新