Django - Static file not found

Pierre de LESPINAY picture Pierre de LESPINAY · May 16, 2011 · Viewed 127.3k times · Source

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 ?

Answer

Pierre de LESPINAY picture Pierre de LESPINAY · May 16, 2011

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:

#STATIC_ROOT = (os.path.join(SITE_ROOT, 'static_files/'))
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = (
  os.path.join(SITE_ROOT, 'static/'),
)

Also don't forget to from django.conf import settings