I've read some posts on similar topics but none of them are exactly the same.
I've a django app (django 2.2.2, python 3.6.12) that works with no problems when served with uwsgi (along with nginx), but has problems of finding static files when served with runserver (python manage.py runserver 0:8000) with error messages like the following:
WARNING Not Found: /static/admin/css/base.css
WARNING Not Found: /static/stylesheets/css/style.css
WARNING Not Found: /static/admin/js/core.js
These WARNINGs are caused by lines in the accessed webpage like the following.
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css">
<script type="text/javascript" src="/static/admin/js/core.js"></script>
Of course the webpage is not displayed correctly due to not being able to access the static files.
I have the following settings:
BASE_PATH = '/data/webapps/myapp'
STATIC_ROOT = os.path.join(BASE_PATH, '_site/static')
and in BASE_PATH, I have folders like the following which contain all files the system complained about not being found:
_site/static/admin/css
_site/static/admin/js
_site/static/stylesheets/css
I even duplicated all folders under _site directly under BASE_PATH like the following, but with no effect.
static/admin/css
static/admin/js
static/stylesheets/css
Here I want to emphasize that it works with no problems when served with uwsgi with the following command:
uwsgi --ini uwsgi.ini
and uwsgi.ini contains the following lines:
[uwsgi]
chdir = %d
enable-threads = true
max-requests = 5000
file = /data/webapps/myapp/myapp/conf/wsgi.py
socket = uwsgi.sock
chmod = 666
chmod-socket = 666
uid = myapp
gid = myapp
and wsgi.py has the following codes:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mybic.conf.settings')
application = get_wsgi_application()
Any help are appreciated.
P.S.
The findstatic command can't find those files either. For example, the following command found no matching.
python manage.py findstatic --verbosity=2 core.js
Adding a vector STATICFILES_DIRS with all folder paths for the static files, such as the following, would only help the findstatic command but not the runserver command, that is, there're still the WARNINGs from accessing the webpage.
STATICFILES_DIRS = [
os.path.join(BASE_PATH, '_site/static/stylesheets/css'),
os.path.join(BASE_PATH, '_site/static/stylesheets/js'),
os.path.join(BASE_PATH, '_site/static/admin/css'),
os.path.join(BASE_PATH, '_site/static/admin/js'),
...
]
Change DEBUG = TRUE
. If it's work just following this post to resolve the problems.
Keep DEBUG = False
, because many security reasons.
Install pip install whitenoise
(current is ver 5.2.0)
In your settings.py
:
'whitenoise.middleware.WhiteNoiseMiddleware',
into MIDDLEWARE
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # Add WhiteNoise here
...
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
bellow your STATIC_ROOT
if DEBUG:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
...
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
manage.py
run python manage.py collectstatic
(For static file of Django Admin site)