Django Will Not Pull CSS Static File (404 Error) Even Though File Paths Look Correct
Question: When I load my local site I cannot get the CSS files to load. I'm running
Django 1.9 and python 3.4.2.

Here is my structure:

    
    
    apps/
        app1/
        app2/
        etc.
    clients/
        media/ #css, js, images, etc.
        static/ #static files
        templates/ #html templates
    __init__.py
    manage.py
    settings.py
    etc.
    

In my settings.py file I have:

    
    
    STATIC_ROOT = os.path.join(BASE_DIR, 'clients', 'static')
    STATIC_URL = 'clients/static/'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'clients', 'media'),
    ]
    MEDIA_ROOT = os.path.join(PROJECT_PATH, 'clients', 'media')
    

And my html template that is calling the css files is as so:

    
    
     {% load staticfiles %}
     <link rel="stylesheet" href="{% static 'css/primary_stylesheet.css' %}" />
    

I continue to get a 404 error that says it can't find the css file
in:/clients/static/css/primary_stylesheet.css

In my settings.py file I have printed out my STATICFILES_DIRS and STATIC_ROOT
and they both lead directly where they should. I've read through the
documentation and tried multiple variations of DIRS and ROOT and don't
understand why the css file is not pulling correctly - even "collectstatic" is
collecting correctly.

I greatly appreciate any help and wisdom someone else has to give.

Thank you!


Answer: 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/'
    

