Reputation: 25
I am encountering an issue with Django and Whitenoise where my static files are not being served correctly, resulting in 404 errors and MIME checking errors.
I have configured my config/settings.py
file with the necessary settings for static files and Whitenoise middleware:
INSTALLED_APPS = [
# some apps
'django.contrib.staticfiles',
# my apps
]
MIDDLEWARE = [
# some middleware
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
STATIC_URL = '/static/'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STORAGES = {
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
However, when I try to access static files such as http://example.com/static/css/style.css and http://example.com/static/js/script.js, I get 404 errors, and the browser console shows MIME type errors:
Refused to apply style from 'http://example.com/static/css/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Refused to execute script from 'http://example.com/static/js/script.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
The logs indicate the files are not found:
Not Found: /static/css/style.css
Not Found: /static/js/script.js
UPD:
logs after I run dokku logs myapp -t
[2024-02-13 21:01:53 +0000] [12] [INFO] Starting gunicorn 21.0.1
[2024-02-13 21:01:53 +0000] [12] [INFO] Listening at: http://0.0.0.0:5000 (12)
[2024-02-13 21:01:53 +0000] [12] [INFO] Using worker: sync
[2024-02-13 21:01:53 +0000] [161] [INFO] Booting worker with pid: 161
172.17.0.1 - - [13/Feb/2024:21:02:39 +0000] "GET / HTTP/1.1" 200 4782 "http://example.com" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
Not Found: /static/css/style.css
172.17.0.1 - - [13/Feb/2024:21:02:39 +0000] "GET /static/css/style.css HTTP/1.1" 404 4297 "http://example.com" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
Not Found: /static/js/script.js
172.17.0.1 - - [13/Feb/2024:21:02:39 +0000] "GET /static/js/script.js HTTP/1.1" 404 4293 "http://example.com" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
Upvotes: 0
Views: 167