[Answered ]-Django Will Not Pull CSS Static File (404 Error) Even Though File Paths Look Correct

1👍

If you have DEBUG = True set then django won’t actually pull your files from the /static/ folder – it finds and collects your staticfiles at runtime when you input the runserver command.

I think you’ll find that if you use the default setting for STATICFILES_FINDERS your app will be able to serve your files:

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

1👍

If you are running your server with python ./manage.py runserver, you need to set urls for both static and media files, check here: https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development

When I am starting a new project, I generally set my urls.py like this:

from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin

url_patterns = [
    url(r'^admin/', admin.site.urls),
    # your url patterns go here
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This requires you to set STATIC_ROOT, STATIC_URL, MEDIA_ROOT and MEDIA_URL in your settings.py:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static_files'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Leave a comment