2
Obligatory reading:
https://docs.djangoproject.com/en/1.7/howto/static-files/
https://docs.djangoproject.com/en/1.7/ref/contrib/staticfiles/
https://docs.djangoproject.com/en/1.7/ref/settings/#static-files
About these settings:
STATIC_URL = 'assets/'
This means that your app should serve static files with assets/{name}
prefix.
I think this is the source of your problem – the initial /
is missing and you’re generating relative links for static resources, which sounds like a Bad Idea.
Now a page at http://yourserver/foo/bar/
would ask for static files via a relative link:
<img src="assets/{name}">
, so your browser would look in /foo/bar/assets/{name}
and find nothing.
You’d normally want an absolute link for static files, so instead you should use STATIC_URL = '/assets/'
and obtain absolute links <img src="/assets/{name}">
.
STATICFILES_DIRS=(BASE_DIR, 'assets')
Okay, so how does Django find the static files? A Django project can have many installed applications, and each application can have its own directory with static files.
So there’s this setting (set by default):
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder")
This means that, on development setup (runserver
), whenever someone asks for http://yourserver/assets/ponies/applejack.jpg
(where /assets/
is your, now hopefully fixed, STATIC_URL
), Django would try to find a file ponies/applejack.jpg
in the following way:
- first by using
FileSystemFinder
that looks in some fixed directories you specify (STATICFILES_DIRS
), - second by using
AppDirectoriesFinder
that looks into thestatic/
subdirectory of each of yourINSTALLED_APPS
.
So if you want to keep your static files per-app, you don’t need to rely on FileSystemFinder
and you can leave STATICFILES_DIRS
empty.
But if you want to keep all the static files in one place, point STATICFILES_DIRS
to any directory like you did. You should still keep AppDirectoriesFinder
for third party apps you use, like the Django Admin, that come with their own static files.
STATIC_ROOT = 'static/'
This is unnecessary for development setup with runserver
because Django dev server serves your static files itself.
For production, however, you probably want your web server (Apache, Nginx, …) or a content delivery network to handle the static file delivery for you. You can then use the collectstatic
command to collect all the static files from everywhere (e.g. all static files your STATICFILES_FINDERS
can find), and put them all in one directory on your machine, from where you can easily pass them on to Nginx or copy over to a CDN.
0
Do you have this in your settings?
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# django.contrib.staticfiles.finders.DefaultStorageFinder',
)