2012-02-13 33 views
1

我传递一个查询ID对象的视图,然后获取该对象,然后调用下面FUNC:DjangoUnicodeDecodeError:“UTF-8”编解码器不能解码位置0x80的字节51

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), \x80\x00O\xb8\x05\xc0\xa8\x06 \x0c\r)' (<type 'str'>) 

但是,当我调用同一个函数在Django壳,这工作正常。我很难过。 我想知道的是为什么: 1.它在shell中而不是在web服务器上运行。 2.我如何使它在使用Django/WSGI的网络服务器上工作。

谢谢。

回答

1

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

您要么修复代码,以免依赖隐式强制,这会对进程使用系统默认编码,或者您重写Apache init环境,以便将LANG设置为UTF-8变体。

尝试使用“Apache UTF-8语言环境”的Google搜索并找到适合您的Apache发行版的方法。

+0

谢谢格雷厄姆。当我在处理代码时,我发现了一个解决方法,将代码逻辑重新分解到模型的实例方法中。出于某种原因,在获取对象然后传递SNMP变量(使用pySnmp)之间出现问题。 所以,每当遇到这种错误,我发现我可以通过将views.py中的逻辑迁移到models.py中来快速修复它。如上所述,它将查找“Apache UTF-8语言环境”。 – imen 2012-04-23 19:34:17

相关问题