静态图像上的DjangoUnicodeDecodeError

时间:2011-01-19 21:18:35

标签: django image

尝试在Django中包含静态图像时出现此错误。

Traceback (most recent call last):
File "/Library/Python/2.6/site-packages/django/core/servers/basehttp.py", line 280, in run
self.result = application(self.environ, self.start_response)
File "/Library/Python/2.6/site-packages/django/core/servers/basehttp.py", line 674, in __call__
return self.application(environ, start_response)
File "/Library/Python/2.6/site-packages/django/core/handlers/wsgi.py", line 245, in __call__
response = middleware_method(request, response)
File "/Users/Josh/djcode/lists/debug_toolbar/middleware.py", line 105, in process_response
smart_unicode(response.content),
File "/Library/Python/2.6/site-packages/django/utils/encoding.py", line 39, in smart_unicode
return force_unicode(s, encoding, strings_only, errors)
File "/Library/Python/2.6/site-packages/django/utils/encoding.py", line 88, in force_unicode
raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: unexpected code byte. You passed in '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00P\x00\x00\x00H\x08\x06\x00\x00\x00a\x94r[\x00\x00\x00\tpHYs\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a\x9c\x18\x00\x00\nOiCCPPhotoshop ICC profile\x00\x00x\xda\x9dSgTS\xe9\x16=\xf7\xde\xf4BK\x88\x80\x94KoR....`\x82' (<type 'str'>)

我用它来提供我的静态媒体:

if settings.DEBUG:
  urlpatterns += patterns('',
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
  )

这是我的settings.py(尝试了带有和不带斜杠的STATIC_ROOT:

STATIC_URL = '/media/'
STATIC_ROOT = '/Users/Josh/djcode/static_media'

CSS和javascript文件工作正常,但图片却没有。 这是我的HTML:<img src="/media/img/new_list_button.png" alt="Make a new list."/>

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的TEMPLATE_CONTEXT_PROCESSORS中有什么? 你不要忘记添加django.core.context_processors.static吗?

http://docs.djangoproject.com/en/dev/howto/static-files/#with-a-context-processor