提供媒体文件,不提供静态文件 [英] Media files are served, static files aren't
问题描述
I'm stuck due to an evergreen issue, static files not served. Conversely the files placed in the MEDIA_ROOT
subtree get served correctly under MEDIA_URL
.
已剥离的 settings.py
:
DEBUG = True
STATIC_URL = '/static/'
STATIC_ROOT = '/home/foo/devel/static'
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/foo/devel/media'
# the following is deprecated but is it seems grappelly requires it
ADMIN_MEDIA_PREFIX = STATIC_URL + "grappelli/"
STATIC_FILES = ()
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
要创建我所做的项目:
$ cd /home/foo/devel/
$ virtualenv testdrive
$ . bin/activate; pip install django; cd testdrive
$ django-admin.py fbtest
并获得了该目录树(已剥离):
and got this directory tree (stripped):
. <-- /home/foo/devel/
├── bin
├── fbtest
│ └── fbtest
│ ├── media
│ │ └── foo.jpg
│ ├── static
│ └────── foo.jpg
├── include
└── lib
STATIC_URL 下的
文件应由Django静态文件自动提供(在我的情况下不是),而其他文件则必须手动处理.所以我将这些行附加到 urls.py
:
Files under STATIC_URL
should be served automatically by Django staticfiles (not in my case), while other files have to be handled manually. So I appended these lines to urls.py
:
import settings
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL.lstrip("/"),
'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
访问 http://host/media/filebrowser/foo.jpg
是有效的,而 http://host/static/foo.jpg
则给出错误404.为什么?
Accessing http://host/media/filebrowser/foo.jpg
works, while http://host/static/foo.jpg
gives error 404. Why?
推荐答案
这是一个愚蠢的错误.我忘记将 fbtest
添加到 INSTALLED_APPS
中,所以静态文件机制无法管理该应用程序的静态文件.
It was a silly error. I forgot to add fbtest
to INSTALLED_APPS
, so the static file machinery didn't manage static files for this app.
这篇关于提供媒体文件,不提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!