1

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!

9
  • Are you sure the file /clients/media/css/primary_stylesheet.css exists? When you run collectstatic have you verified that /clients/static/css/primary_stylesheet.css exists? Commented Jul 17, 2016 at 2:12
  • Please add your STATICFILES_FINDERS setting as well. Commented Jul 17, 2016 at 2:12
  • @YPCrumble Yes I am sure that /clients/media/css/primary_stylehseets.css exists. And if I delete the CSS file from my /static/ file, and then collectstatic again, the file is properly updated in /static/css/ Commented Jul 17, 2016 at 2:30
  • @YPCrumble Currently I have no STATICFILES_FINDERS setting listed...Is this a requirement? Commented Jul 17, 2016 at 2:34
  • What server are you using to serve this django application? Commented Jul 17, 2016 at 5:47

2 Answers 2

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',
]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. I added 'STATICFILES_FINDERS = [django.contrib.staticfiles.finders.FileSystemFinder,]' and my media files were still not loaded. I have not added '...AppDirectoriesFinder' to the array because I was under the impression that it would affect nothing because my staticfiles to be collected are not located inside of an app. I will try adding it this evening and see if anything changes...
@Tarrant would still have to be STATICFILES_FINDERS with the S.
Sorry spelling mistake - udpated!
yes for whatever reason adding 'django.contrib.staticfiles.finders.AppDirectoriesFinder' to my STATICFILES_FINDERS array worked.Thank you for your help I appreciate it!
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/'

2 Comments

I will try includin ga MEDIA_URL and add 'if setting.DEBUG:...' this evening and let you know if that fixes things.
FYI this did not fix the problem, but none the less I appreciate the url advice!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.