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?
This problem is realy evergreen... Some hints:
My settings for django 1.4 (no grappelli):
urls.py
settings.py
It was a silly error. I forgot to add
fbtest
toINSTALLED_APPS
, so the static file machinery didn't manage static files for this app.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 inSTATICFILES_DIRS
available atSTATIC_URL
. You don't actually manually put anything inSTATIC_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 thecollectstatic
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 asSTATIC_ROOT
orMEDIA_ROOT
) and then add that directory toSTATICFILES_DIRS
. For example, I normally call mine "assets", so: