Warm tip: This article is reproduced from serverfault.com, please click

Static file not found

发布于 2011-05-16 08:14:19

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 ?

Questioner
Pierre de LESPINAY
Viewed
0
Pierre de LESPINAY 2014-12-05 15:59:29

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