Usually I have this issue exactly the other way around!
In my development environment my Django app will not load some of my static files, specifically ones that I have added myself: that is, the two packages I've added to my app (admin
and ckeditor
) are both loading up fine, but two of the folders I've created and linked myself (img
and css
) are not being found. Here's a map of my directory:
root
|-- blog (this is the name of my app)
|-- mysite (name of my site)
|-- settings.py
|-- urls.py
|-- media
|-- static
|-- admin
|-- ckeditor
|-- css
|-- img
As stated, ckeditor
and admin
load fine while the others do not. Here's an example from the runserver
output in debug mode (the file at static/css/base.css
exists in my file tree):
GET /static/ckeditor/ckeditor/ckeditor.js HTTP/1.1" 200 690627
GET /static/admin/css/fonts.css HTTP/1.1" 200 423
GET /static/admin/css/widgets.css HTTP/1.1" 200 10340
GET /static/css/base.css HTTP/1.1" 404 1761
GET /static/img/brand.png HTTP/1.1" 404 1764
Here's some other information which may be of interest:
admin
and ckeditor
work.{% load static %}
as instructed by the Django docs. In older versions I used {% load staticfiles %}
and I've tried that too.collectstatic
in both environments.DEBUG=False
works fine in production (all the static files load) but no static files load at all when DEBUG=False
in development. This is to be expected though, since in development I don't have a web server to handle this (to clarify, I usually run the server in debug mode, but have tried turning this setting off and on to see what changes occur)In an effort to help anyone debug my problem, here are some relevant excerpts files:
settings.py
DEBUG = True
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'ckeditor',
'ckeditor_uploader',
]
...
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('blog.urls')),
path('admin/', admin.site.urls),
path('ckeditor', include('ckeditor_uploader.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Since you have 'django.contrib.staticfiles'
in your apps list, Django actually does NOT load files from STATIC_ROOT when DEBUG is True.
When DEBUG is enabled Django uses STATICFILES_FINDERS option that by default is:
[
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
FileSystemFinder will find files stored in the STATICFILES_DIRS setting
and AppDirectoriesFinder is looking for files in a static
subdirectory of each app.
That's why folders admin and ckeditor work - Django doesn't use your /root/static folder at all when DEBUG is True. Instead it gets files by AppDirectoriesFinder from apps static
subdirectories.
Something like .../3.5/lib/python3.5/site-packages/django-ckeditor/static/...
When you run collectstatic
command it collects all static files that STATICFILES_FINDERS can find and copies them into your STATIC_ROOT (/root/static/) and if DEBUG is False Django just uses this folder instead of STATICFILES_FINDERS
So how you can make it work with debug enabled. Just create a folder and add it to STATICFILES_DIRS like this:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'some_new_static_folder'),
]
Also you can delete all files from /root/static/
you don't need to create anything inside this folder manually, collectstatic
will do it automatically for production.