skip to Main Content

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'),
    ...
]

2

Answers


  1. Chosen as BEST ANSWER

    As @NguyễnVũThiên pointed out, it has something to do with debug mode. When debug is turned off (either set DEBUG to False or not setting DEBUG at all), runserver will no longer serve static files. Unfortunately the WARNINGs are quite misleading. Instead of saying Not found, they should say Not served (and say something about debug mode).

    So to resolve the problems, as suggested in the link @NguyễnVũThiên gave, either turn on the debug mode (DEBUG = True) or add the --insecure option to runserver (python manage.py runserver --insecure).


  2. 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:

    1. Add 'whitenoise.middleware.WhiteNoiseMiddleware', into MIDDLEWARE
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'whitenoise.middleware.WhiteNoiseMiddleware', # Add WhiteNoise here
        ...
    ]
    
    1. Add 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'
    
    
    1. In your folder which contain manage.py run python manage.py collectstatic (For static file of Django Admin site)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search