Media files are served, static files aren't

2019-07-30 19:28发布

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.

Stripped 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',
)

To create the project I did:

$ 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

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,
        }),
    )

Accessing http://host/media/filebrowser/foo.jpg works, while http://host/static/foo.jpg gives error 404. Why?

3条回答
看我几分像从前
2楼-- · 2019-07-30 19:55

This problem is realy evergreen... Some hints:

TEMPLATE_CONTEXT_PROCESSORS = (
    # ...
    'django.core.context_processors.static',
    # ...
)

INSTALLED_APPS = (
    # ...
    'django.contrib.staticfiles',
    # ... 
)

My settings for django 1.4 (no grappelli):

urls.py

if settings.DEBUG:
    urlpatterns = patterns('',

        url(r'^%s(?P<path>.*)$' % settings.STATIC_URL.lstrip('/'), 'django.views.static.serve',
            {'document_root': settings.STATIC_ROOT, "show_indexes": True}),

        url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL.lstrip('/'), 'django.views.static.serve',
            {'document_root': settings.MEDIA_ROOT, "show_indexes": True}),

    ) + urlpatterns

settings.py

MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'


TEMPLATE_CONTEXT_PROCESSORS = (
    # ...
    'django.core.context_processors.static',
    # ...
)

INSTALLED_APPS = (
    # ...
    # 'django.contrib.staticfiles',
    # ...
)
查看更多
我只想做你的唯一
3楼-- · 2019-07-30 20:10

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.

查看更多
别忘想泡老子
4楼-- · 2019-07-30 20:16

Files under STATIC_URL should be served automatically by Django staticfiles (not in my case), while other files have to be handled manually.

That's incorrect. Django never serves STATIC_ROOT ever -- not even in development. What it does do is make files in each app's "static" directory and files in any directory specified in STATICFILES_DIRS available at STATIC_URL. You don't actually manually put anything in STATIC_ROOT ever; in fact, in development, you shouldn't even have the directory there. Put simply, STATIC_ROOT is only a dumping ground for your static files in production when you run the collectstatic management command.

In development, all static files should go into someapp/static, where "someapp" is the app they apply to. In the case that the files apply to the project as a whole, a global CSS file for example, you need to create an entirely different directory (i.e. not the same as STATIC_ROOT or MEDIA_ROOT) and then add that directory to STATICFILES_DIRS. For example, I normally call mine "assets", so:

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(__file__), 'assets'),
)
查看更多
登录 后发表回答