Search code examples
pythondjangodjango-staticfilescollectstatic

strange django static file behaviour


I'm getting 404 for static files when DEBUG=False in my settings. The file is served fine when DEBUG=True.

I'm looking at the docs and all my configs seem OK to me.

# my_settings.py
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')

STATIC_URL = '/static/`

then I run collectstatic --settings=my_settings, and ls static to confirm it has collected the files. It has:

ls ls static/css/core.css
# (its there).

But when we request "localhost:8000/static/css/core.css" we get 404.

Note running python manage.py findstatic css/core.css --settings=my_settings fails to find the file. When I do DEBUG=True and run findstatic it finds the file, but in the sub app's static dir, not the collected directory (STATIC_ROOT).

Note I have:

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

and "staticfiles" is in my INSTALLED_APPS.

Django 1.6


Solution

  • As you have specified DEBUG = FALSE in settings, its now left on HTTP server like nginx or apache to serve the static files. You need to configure your webserver to serve static files. An example for apache can be found here.