0

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'),
    ...
]
Shiping
  • 1,203
  • 2
  • 11
  • 21
  • Please add template code where you refer these files. And output of `collectstatic` command. – Ivan Starostin Mar 27 '21 at 20:09
  • Also please tell, do your own static files work? Only `admin` static files are broken or any other too? – Ivan Starostin Mar 27 '21 at 20:12
  • @IvanStarostin Thanks for the comments. All files under _site/static are collected by the collectstatic command plus files I created manually. And all those files in the WARNINGs are present under both BASE_PATH/_site/static/ and BASE_PATH/static/. I've revised my post. – Shiping Mar 28 '21 at 15:21
  • My guess is something overwrites STATIC_ROOT somewhere. Or some troubles with expected/actual current working folder. – Ivan Starostin Mar 28 '21 at 16:08
  • Please tell your status Debug, `false` or `true`? If you change **debug = 'true'** and then static file it's work, maybe you need to see this [post] (https://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail) – Nguyễn Vũ Thiên Mar 29 '21 at 00:42
  • 1
    @NguyễnVũThiên thanks. that resolved the issue. – Shiping Mar 29 '21 at 14:32

2 Answers2

2

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).

Shiping
  • 1,203
  • 2
  • 11
  • 21
  • Change `debug = True` just a test for problem. You need turn off debug in production. So be careful. – Nguyễn Vũ Thiên Mar 29 '21 at 22:45
  • For the love of the effort people put on SO, it could be appreciative to accept & or upvote @NguyễnVũThiên's answer since it helped you. Cheers! – Laenka-Oss Apr 05 '21 at 12:51
  • --insecure was the option I was looking for in development. Thanks a lot! – OmerFI Jan 20 '23 at 12:01
  • @Laenka-Oss sorry I didn't keep tracking of this trend after I posted my answer based on NguyễnVũThiên's suggestion. Of course I agree with you. – Shiping Jan 22 '23 at 01:29
1

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)
Nguyễn Vũ Thiên
  • 761
  • 1
  • 9
  • 19