DjangoUnicodeDecodeError:'utf-8'编解码器无法解码位置4-5中的字节:无效数据

时间:2013-06-03 09:30:56

标签: python django character-encoding

Jython和Django 中,我正在尝试使用此param name="@#~½½¬}0.jpg"

调用位于不同视图“normalize_name”的视图功能

我的电话是:

 documentName = str(normalize_name(name))

我无法理解的是,如果在同一视图中将该函数称为

,则工作原理

调用的函数是:

def normalize_name(value):
  ext = value.split('.')[-1]
  value = join(value.split('.')[0:-1], '.')
  value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
  value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
  value = unicode(re.sub('[-\s]+', '-', value))

  return value + '.' + ext

我得到的错误:

Traceback (most recent call last):
  File "/usr/share/jython-env-django/Lib/wsgiref/handlers.py", line 92, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/share/jython-env-django/Lib/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
    return self.application(environ, start_response)
  File "/usr/share/jython-env-django/Lib/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
     response = self.get_response(request)
  File "/usr/share/jython-env-django/Lib/site-packages/django/core/handlers/base.py", line 179, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/share/jython-env-django/Lib/site-packages/django/core/handlers/base.py", line 221, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
  File "/usr/share/jython-env-django/Lib/site-packages/django/views/debug.py", line 63, in technical_500_response
    text = reporter.get_traceback_text()
  File "/usr/share/jython-env-django/Lib/site-packages/django/views/debug.py", line 281, in get_traceback_text
    c = Context(self.get_traceback_data(), autoescape=False)
  File "/usr/share/jython-env-django/Lib/site-packages/django/views/debug.py", line 236, in get_traceback_data
    frame['vars'] = [(k, force_escape(pprint(v))) for k, v in frame['vars']]
  File "/usr/share/jython-env-django/Lib/site-packages/django/template/defaultfilters.py", line 39, in _dec
    args[0] = force_unicode(args[0])
  File "/usr/share/jython-env-django/Lib/site-packages/django/utils/encoding.py", line 93, in force_unicode
    raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'utf-8' codec can't decode bytes in position 4-5: invalid     data. You passed in "'@#~\xc2\\xbd\xc2\\xbd\xc2\\xac}0.jpg'" (<type 'str'>)

提前谢谢

0 个答案:

没有答案