I've seen several posts for this issue but didn't found my solution.
I'm trying to serve static files within my Django 1.3 development environment.
Here are my settings
...
STATIC_ROOT = '/home/glide/Documents/django/cbox/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/static/',
)
...
My urls.py
urlpatterns = patterns('',
...
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root', settings.STATIC_ROOT}
),
...
);
My /home/glide/Documents/django/cbox/static/ directory is like
css
main.css
javascript
image
I get a 404 error when trying to access http://127.0.0.1:8000/static/css/main.css.
Do I have to specify patterns for css, javascript and images individually ?
Serving static files can be achieved in several ways; here are my notes to self:
static/my_app/
directory tomy_app
(see the note about namespacing below)The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
)I prefer the first way, and a setup that's close to the way defined in the documentation, so in order to serve the file
admin-custom.css
to override a couple of admin styles, I have a setup like so:This is then used in the template like so:
When deploying, I run
collectstatic
and serve static files with nginx.The docs which cleared up all the confusion for me:
Another error can be not having your app listed in the
INSTALLED_APPS
listing like:Without having it in, you can face problems like not detecting your static files, basically all the files involving your app. Even though it can be correct as suggested in the correct answer by using:
Can be one of the errors and should be reviewed if getting this error.
There could be only two things in
settings.py
file those makes your static files serve.1)
STATIC_URL = '/static/'
2)
and your static files should lie under static directory which is in same directory as project's settings file.
Even then if your static files are not loading then reason is , you might have kept
change it to True (strictly for development only). In production just change
STATICFILES_DIRS
to whatever path where static files resides.{'document_root', settings.STATIC_ROOT}
needs to be{'document_root': settings.STATIC_ROOT}
or you'll get an error like
dictionary update sequence element #0 has length 6; 2 is required
I confused STATIC_ROOT and STATICFILES_DIRS
Actually I was not really understanding the utility of STATIC_ROOT. I thought that it was the directory on which I have to put my common files. This directory is used for the production, this is the directory on which static files will be put (collected) by collectstatic.
STATICFILES_DIRS is the one that I need.
Since I'm in a development environment, the solution for me is to not use STATIC_ROOT (or to specify another path) and set my common files directory in STATICFILES_DIRS:
Also don't forget to
from django.conf import settings