Django 2.1 static files loading in production but not developmentWhere Are My Django Static Files?How to override django static file development server?django media files on developcannot access static files in Djangodjango - having some issue serving static_url files, but media_url works okLink to a django static file doesnt workDjango 1.8.3 urlsHow to link my css, js and image file link in djangoDjango Static image File Failed to load resourceDjango admin- static files not served- Digital Ocean
Is there a hemisphere-neutral way of specifying a season?
Plagiarism or not?
Apex Framework / library for consuming REST services
How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)
How writing a dominant 7 sus4 chord in RNA ( Vsus7 chord in the 1st inversion)
What is the idiomatic way to say "clothing fits"?
Why is consensus so controversial in Britain?
Expand and Contract
In 'Revenger,' what does 'cove' come from?
How can saying a song's name be a copyright violation?
What type of content (depth/breadth) is expected for a short presentation for Asst Professor interview in the UK?
Could the museum Saturn V's be refitted for one more flight?
Alternative to sending password over mail?
Which is the best way to check return result?
ssTTsSTtRrriinInnnnNNNIiinngg
Should I tell management that I intend to leave due to bad software development practices?
Unlock My Phone! February 2018
Can my sorcerer use a spellbook only to collect spells and scribe scrolls, not cast?
Why can't we play rap on piano?
What mechanic is there to disable a threat instead of killing it?
Do scales need to be in alphabetical order?
What killed these X2 caps?
How dangerous is XSS?
How seriously should I take size and weight limits of hand luggage?
Django 2.1 static files loading in production but not development
Where Are My Django Static Files?How to override django static file development server?django media files on developcannot access static files in Djangodjango - having some issue serving static_url files, but media_url works okLink to a django static file doesnt workDjango 1.8.3 urlsHow to link my css, js and image file link in djangoDjango Static image File Failed to load resourceDjango admin- static files not served- Digital Ocean
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:
- It works fine in production! I assumed this was because I had dedicated aliases in my apache config, but that doesn't explain why
admin
andckeditor
work. - I have routed media in much the same way (see the settings file below) and that works fine in development.
- I am using the template tag
% load static %
as instructed by the Django docs. In older versions I used% load staticfiles %
and I've tried that too. - I have run
collectstatic
in both environments. - Running with
DEBUG=False
works fine in production (all the static files load) but no static files load at all whenDEBUG=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)
django python-3.x static-files
|
show 3 more comments
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:
- It works fine in production! I assumed this was because I had dedicated aliases in my apache config, but that doesn't explain why
admin
andckeditor
work. - I have routed media in much the same way (see the settings file below) and that works fine in development.
- I am using the template tag
% load static %
as instructed by the Django docs. In older versions I used% load staticfiles %
and I've tried that too. - I have run
collectstatic
in both environments. - Running with
DEBUG=False
works fine in production (all the static files load) but no static files load at all whenDEBUG=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)
django python-3.x static-files
1
Have you runcollectstatic
?
– nik_m
Mar 3 at 19:39
@nik_m yes, in both environments - I'll add that to the question
– Jack Parkinson
Mar 3 at 20:53
in your urls.py the ckeditor path does not have a/
try giving it and see.
– Sammy J
Mar 7 at 10:47
The static files forckeditor
are already being loaded fine in both environments. Adding the/
seems to have no effect, on either theckeditor
's static files or on the static files which are not being loaded.
– Jack Parkinson
Mar 7 at 18:00
ok, then one more suggestion for the static_root and media_root,STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
andMEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
try this.
– Sammy J
Mar 8 at 7:17
|
show 3 more comments
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:
- It works fine in production! I assumed this was because I had dedicated aliases in my apache config, but that doesn't explain why
admin
andckeditor
work. - I have routed media in much the same way (see the settings file below) and that works fine in development.
- I am using the template tag
% load static %
as instructed by the Django docs. In older versions I used% load staticfiles %
and I've tried that too. - I have run
collectstatic
in both environments. - Running with
DEBUG=False
works fine in production (all the static files load) but no static files load at all whenDEBUG=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)
django python-3.x static-files
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:
- It works fine in production! I assumed this was because I had dedicated aliases in my apache config, but that doesn't explain why
admin
andckeditor
work. - I have routed media in much the same way (see the settings file below) and that works fine in development.
- I am using the template tag
% load static %
as instructed by the Django docs. In older versions I used% load staticfiles %
and I've tried that too. - I have run
collectstatic
in both environments. - Running with
DEBUG=False
works fine in production (all the static files load) but no static files load at all whenDEBUG=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)
django python-3.x static-files
django python-3.x static-files
edited Mar 8 at 22:22
Jack Parkinson
asked Mar 3 at 19:31
Jack ParkinsonJack Parkinson
347624
347624
1
Have you runcollectstatic
?
– nik_m
Mar 3 at 19:39
@nik_m yes, in both environments - I'll add that to the question
– Jack Parkinson
Mar 3 at 20:53
in your urls.py the ckeditor path does not have a/
try giving it and see.
– Sammy J
Mar 7 at 10:47
The static files forckeditor
are already being loaded fine in both environments. Adding the/
seems to have no effect, on either theckeditor
's static files or on the static files which are not being loaded.
– Jack Parkinson
Mar 7 at 18:00
ok, then one more suggestion for the static_root and media_root,STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
andMEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
try this.
– Sammy J
Mar 8 at 7:17
|
show 3 more comments
1
Have you runcollectstatic
?
– nik_m
Mar 3 at 19:39
@nik_m yes, in both environments - I'll add that to the question
– Jack Parkinson
Mar 3 at 20:53
in your urls.py the ckeditor path does not have a/
try giving it and see.
– Sammy J
Mar 7 at 10:47
The static files forckeditor
are already being loaded fine in both environments. Adding the/
seems to have no effect, on either theckeditor
's static files or on the static files which are not being loaded.
– Jack Parkinson
Mar 7 at 18:00
ok, then one more suggestion for the static_root and media_root,STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
andMEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
try this.
– Sammy J
Mar 8 at 7:17
1
1
Have you run
collectstatic
?– nik_m
Mar 3 at 19:39
Have you run
collectstatic
?– nik_m
Mar 3 at 19:39
@nik_m yes, in both environments - I'll add that to the question
– Jack Parkinson
Mar 3 at 20:53
@nik_m yes, in both environments - I'll add that to the question
– Jack Parkinson
Mar 3 at 20:53
in your urls.py the ckeditor path does not have a
/
try giving it and see.– Sammy J
Mar 7 at 10:47
in your urls.py the ckeditor path does not have a
/
try giving it and see.– Sammy J
Mar 7 at 10:47
The static files for
ckeditor
are already being loaded fine in both environments. Adding the /
seems to have no effect, on either the ckeditor
's static files or on the static files which are not being loaded.– Jack Parkinson
Mar 7 at 18:00
The static files for
ckeditor
are already being loaded fine in both environments. Adding the /
seems to have no effect, on either the ckeditor
's static files or on the static files which are not being loaded.– Jack Parkinson
Mar 7 at 18:00
ok, then one more suggestion for the static_root and media_root,
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
and MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
try this.– Sammy J
Mar 8 at 7:17
ok, then one more suggestion for the static_root and media_root,
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
and MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
try this.– Sammy J
Mar 8 at 7:17
|
show 3 more comments
4 Answers
4
active
oldest
votes
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.
add a comment |
This is an easy approach for handling static files in django (that works out of the box if you use Django default options):
Don't ever put anything yourself into the folder you specify asSTATIC_ROOT
.- Put static files specific to an app into the folder
static
inside the app. - For static files that do not directly belong to an app, create the folder
static_files
within your project and add to your settings:STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_files'),]
. Obviously you can choose another name,static_files
is just a suggestion.
For production runcollectstatic
so that Django collects your static files (from points 2. and 3.) and puts them into the folder you created in 1.
If you are in debugging mode you are done after step 3.
In your case, the problem was that you put static content into the STATIC_ROOT
which is a folder Django wont look for content in debugging mode.admin
and ckeditor
work because they follow step 2., thus their static files actually come from the folder of the installed app and not from your static
folder when in debugging mode.
So, here is how to fix your issue:
- do step 3. from above.
- move your folders
img
andcss
to the folder created in step 3. - (optional) wipe your
STATIC_ROOT
folder.
add a comment |
Your urlpatterns are correct, which means the issue is most likely that settings.DEBUG = False
in the settings file. Make sure settings.DEBUG
is True
, and the static files should load. You can verify this by either using manage.py shell
and checking the value of the DEBUG
flag.
The only other issues to look for is whether you are pointing to the correct urls file in your settings by verifying the ROOT_URLCONF
setting and whether your BASE_DIR
points to the parent directory of the static
directory. Both values can also be verified in shell
.
For example, if your settings file is located in /path/to/root/blog/settings.py
, then you set BASE_DIR
as :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Forgive me for not being clear: although I talk aboutsettings.DEBUG
beingFalse
in the additional information, it is usually set toTrue
in development. In the case mentioned above, I tried changing it toFalse
to see what effect it would have, and the behaviour was essentially as expected. I'll be sure to clarify this in the question. To confirm, I have checked themanage.py shell
and also forced a 404 error while running the server: both indicatedDEBUG=True
. Further, myBASE_DIR
exactly the same as you suggest - I'll make sure the question reflects this too.
– Jack Parkinson
Mar 8 at 22:19
add a comment |
I also had some issues with local files and this help my situation.
I do store my static files on S3 but you can adjust your image location URL.
if DEBUG == False:
STATICFILES_LOCATION = 'static'
STATIC_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
else:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54972837%2fdjango-2-1-static-files-loading-in-production-but-not-development%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 9 at 12:11
answered Mar 9 at 11:44
MrBinWinMrBinWin
4841721
4841721
add a comment |
add a comment |
This is an easy approach for handling static files in django (that works out of the box if you use Django default options):
Don't ever put anything yourself into the folder you specify asSTATIC_ROOT
.- Put static files specific to an app into the folder
static
inside the app. - For static files that do not directly belong to an app, create the folder
static_files
within your project and add to your settings:STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_files'),]
. Obviously you can choose another name,static_files
is just a suggestion.
For production runcollectstatic
so that Django collects your static files (from points 2. and 3.) and puts them into the folder you created in 1.
If you are in debugging mode you are done after step 3.
In your case, the problem was that you put static content into the STATIC_ROOT
which is a folder Django wont look for content in debugging mode.admin
and ckeditor
work because they follow step 2., thus their static files actually come from the folder of the installed app and not from your static
folder when in debugging mode.
So, here is how to fix your issue:
- do step 3. from above.
- move your folders
img
andcss
to the folder created in step 3. - (optional) wipe your
STATIC_ROOT
folder.
add a comment |
This is an easy approach for handling static files in django (that works out of the box if you use Django default options):
Don't ever put anything yourself into the folder you specify asSTATIC_ROOT
.- Put static files specific to an app into the folder
static
inside the app. - For static files that do not directly belong to an app, create the folder
static_files
within your project and add to your settings:STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_files'),]
. Obviously you can choose another name,static_files
is just a suggestion.
For production runcollectstatic
so that Django collects your static files (from points 2. and 3.) and puts them into the folder you created in 1.
If you are in debugging mode you are done after step 3.
In your case, the problem was that you put static content into the STATIC_ROOT
which is a folder Django wont look for content in debugging mode.admin
and ckeditor
work because they follow step 2., thus their static files actually come from the folder of the installed app and not from your static
folder when in debugging mode.
So, here is how to fix your issue:
- do step 3. from above.
- move your folders
img
andcss
to the folder created in step 3. - (optional) wipe your
STATIC_ROOT
folder.
add a comment |
This is an easy approach for handling static files in django (that works out of the box if you use Django default options):
Don't ever put anything yourself into the folder you specify asSTATIC_ROOT
.- Put static files specific to an app into the folder
static
inside the app. - For static files that do not directly belong to an app, create the folder
static_files
within your project and add to your settings:STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_files'),]
. Obviously you can choose another name,static_files
is just a suggestion.
For production runcollectstatic
so that Django collects your static files (from points 2. and 3.) and puts them into the folder you created in 1.
If you are in debugging mode you are done after step 3.
In your case, the problem was that you put static content into the STATIC_ROOT
which is a folder Django wont look for content in debugging mode.admin
and ckeditor
work because they follow step 2., thus their static files actually come from the folder of the installed app and not from your static
folder when in debugging mode.
So, here is how to fix your issue:
- do step 3. from above.
- move your folders
img
andcss
to the folder created in step 3. - (optional) wipe your
STATIC_ROOT
folder.
This is an easy approach for handling static files in django (that works out of the box if you use Django default options):
Don't ever put anything yourself into the folder you specify asSTATIC_ROOT
.- Put static files specific to an app into the folder
static
inside the app. - For static files that do not directly belong to an app, create the folder
static_files
within your project and add to your settings:STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_files'),]
. Obviously you can choose another name,static_files
is just a suggestion.
For production runcollectstatic
so that Django collects your static files (from points 2. and 3.) and puts them into the folder you created in 1.
If you are in debugging mode you are done after step 3.
In your case, the problem was that you put static content into the STATIC_ROOT
which is a folder Django wont look for content in debugging mode.admin
and ckeditor
work because they follow step 2., thus their static files actually come from the folder of the installed app and not from your static
folder when in debugging mode.
So, here is how to fix your issue:
- do step 3. from above.
- move your folders
img
andcss
to the folder created in step 3. - (optional) wipe your
STATIC_ROOT
folder.
edited Mar 12 at 14:31
answered Mar 9 at 13:01
jojojojo
4,06422546
4,06422546
add a comment |
add a comment |
Your urlpatterns are correct, which means the issue is most likely that settings.DEBUG = False
in the settings file. Make sure settings.DEBUG
is True
, and the static files should load. You can verify this by either using manage.py shell
and checking the value of the DEBUG
flag.
The only other issues to look for is whether you are pointing to the correct urls file in your settings by verifying the ROOT_URLCONF
setting and whether your BASE_DIR
points to the parent directory of the static
directory. Both values can also be verified in shell
.
For example, if your settings file is located in /path/to/root/blog/settings.py
, then you set BASE_DIR
as :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Forgive me for not being clear: although I talk aboutsettings.DEBUG
beingFalse
in the additional information, it is usually set toTrue
in development. In the case mentioned above, I tried changing it toFalse
to see what effect it would have, and the behaviour was essentially as expected. I'll be sure to clarify this in the question. To confirm, I have checked themanage.py shell
and also forced a 404 error while running the server: both indicatedDEBUG=True
. Further, myBASE_DIR
exactly the same as you suggest - I'll make sure the question reflects this too.
– Jack Parkinson
Mar 8 at 22:19
add a comment |
Your urlpatterns are correct, which means the issue is most likely that settings.DEBUG = False
in the settings file. Make sure settings.DEBUG
is True
, and the static files should load. You can verify this by either using manage.py shell
and checking the value of the DEBUG
flag.
The only other issues to look for is whether you are pointing to the correct urls file in your settings by verifying the ROOT_URLCONF
setting and whether your BASE_DIR
points to the parent directory of the static
directory. Both values can also be verified in shell
.
For example, if your settings file is located in /path/to/root/blog/settings.py
, then you set BASE_DIR
as :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Forgive me for not being clear: although I talk aboutsettings.DEBUG
beingFalse
in the additional information, it is usually set toTrue
in development. In the case mentioned above, I tried changing it toFalse
to see what effect it would have, and the behaviour was essentially as expected. I'll be sure to clarify this in the question. To confirm, I have checked themanage.py shell
and also forced a 404 error while running the server: both indicatedDEBUG=True
. Further, myBASE_DIR
exactly the same as you suggest - I'll make sure the question reflects this too.
– Jack Parkinson
Mar 8 at 22:19
add a comment |
Your urlpatterns are correct, which means the issue is most likely that settings.DEBUG = False
in the settings file. Make sure settings.DEBUG
is True
, and the static files should load. You can verify this by either using manage.py shell
and checking the value of the DEBUG
flag.
The only other issues to look for is whether you are pointing to the correct urls file in your settings by verifying the ROOT_URLCONF
setting and whether your BASE_DIR
points to the parent directory of the static
directory. Both values can also be verified in shell
.
For example, if your settings file is located in /path/to/root/blog/settings.py
, then you set BASE_DIR
as :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Your urlpatterns are correct, which means the issue is most likely that settings.DEBUG = False
in the settings file. Make sure settings.DEBUG
is True
, and the static files should load. You can verify this by either using manage.py shell
and checking the value of the DEBUG
flag.
The only other issues to look for is whether you are pointing to the correct urls file in your settings by verifying the ROOT_URLCONF
setting and whether your BASE_DIR
points to the parent directory of the static
directory. Both values can also be verified in shell
.
For example, if your settings file is located in /path/to/root/blog/settings.py
, then you set BASE_DIR
as :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
edited Mar 8 at 17:33
answered Mar 8 at 17:27
2ps2ps
8,11721131
8,11721131
Forgive me for not being clear: although I talk aboutsettings.DEBUG
beingFalse
in the additional information, it is usually set toTrue
in development. In the case mentioned above, I tried changing it toFalse
to see what effect it would have, and the behaviour was essentially as expected. I'll be sure to clarify this in the question. To confirm, I have checked themanage.py shell
and also forced a 404 error while running the server: both indicatedDEBUG=True
. Further, myBASE_DIR
exactly the same as you suggest - I'll make sure the question reflects this too.
– Jack Parkinson
Mar 8 at 22:19
add a comment |
Forgive me for not being clear: although I talk aboutsettings.DEBUG
beingFalse
in the additional information, it is usually set toTrue
in development. In the case mentioned above, I tried changing it toFalse
to see what effect it would have, and the behaviour was essentially as expected. I'll be sure to clarify this in the question. To confirm, I have checked themanage.py shell
and also forced a 404 error while running the server: both indicatedDEBUG=True
. Further, myBASE_DIR
exactly the same as you suggest - I'll make sure the question reflects this too.
– Jack Parkinson
Mar 8 at 22:19
Forgive me for not being clear: although I talk about
settings.DEBUG
being False
in the additional information, it is usually set to True
in development. In the case mentioned above, I tried changing it to False
to see what effect it would have, and the behaviour was essentially as expected. I'll be sure to clarify this in the question. To confirm, I have checked the manage.py shell
and also forced a 404 error while running the server: both indicated DEBUG=True
. Further, my BASE_DIR
exactly the same as you suggest - I'll make sure the question reflects this too.– Jack Parkinson
Mar 8 at 22:19
Forgive me for not being clear: although I talk about
settings.DEBUG
being False
in the additional information, it is usually set to True
in development. In the case mentioned above, I tried changing it to False
to see what effect it would have, and the behaviour was essentially as expected. I'll be sure to clarify this in the question. To confirm, I have checked the manage.py shell
and also forced a 404 error while running the server: both indicated DEBUG=True
. Further, my BASE_DIR
exactly the same as you suggest - I'll make sure the question reflects this too.– Jack Parkinson
Mar 8 at 22:19
add a comment |
I also had some issues with local files and this help my situation.
I do store my static files on S3 but you can adjust your image location URL.
if DEBUG == False:
STATICFILES_LOCATION = 'static'
STATIC_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
else:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
add a comment |
I also had some issues with local files and this help my situation.
I do store my static files on S3 but you can adjust your image location URL.
if DEBUG == False:
STATICFILES_LOCATION = 'static'
STATIC_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
else:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
add a comment |
I also had some issues with local files and this help my situation.
I do store my static files on S3 but you can adjust your image location URL.
if DEBUG == False:
STATICFILES_LOCATION = 'static'
STATIC_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
else:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
I also had some issues with local files and this help my situation.
I do store my static files on S3 but you can adjust your image location URL.
if DEBUG == False:
STATICFILES_LOCATION = 'static'
STATIC_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
else:
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
answered Mar 9 at 12:22
WayBehindWayBehind
1,0971835
1,0971835
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54972837%2fdjango-2-1-static-files-loading-in-production-but-not-development%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you run
collectstatic
?– nik_m
Mar 3 at 19:39
@nik_m yes, in both environments - I'll add that to the question
– Jack Parkinson
Mar 3 at 20:53
in your urls.py the ckeditor path does not have a
/
try giving it and see.– Sammy J
Mar 7 at 10:47
The static files for
ckeditor
are already being loaded fine in both environments. Adding the/
seems to have no effect, on either theckeditor
's static files or on the static files which are not being loaded.– Jack Parkinson
Mar 7 at 18:00
ok, then one more suggestion for the static_root and media_root,
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
andMEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
try this.– Sammy J
Mar 8 at 7:17