Home:ALL Converter>I keep getting a 404 error when django is looking for static files

I keep getting a 404 error when django is looking for static files

Ask Time:2019-07-29T16:27:17         Author:12944qwerty

Json Formatter

I keep getting a 404 error when django is looking for static files.

settings.py

STATIC_ROOT = os.path.join(BASE_DIR, 'business', 'static_cdn')
STATIC_URL = '/static_cdn/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'business', 'static'),
]

project tree structure of app

- business
-- migrations
--- ....
-- static_cdn
--- business
---- main.css
---- scripts.js
-- templates
--- business
---- base.html
---- home.html
-- templatetags
--- ...
-- __init__.py
-- admin.py
-- apps.py
-- models.py
-- tests.py
-- urls.py
-- views.py

Error

[29/Jul/2019 13:09:45] "GET / HTTP/1.1" 200 12670
[29/Jul/2019 13:09:45] "GET /static_cdn/business/main.css HTTP/1.1" 404 77
[29/Jul/2019 13:09:45] "GET /static_cdn/business/scripts.js HTTP/1.1" 404 77

I also link to static files like {% static 'business/main.css' %} and I do have {% load static %} at the top of my document.

Why isn't django able to find the static files? The error says it is looking in the correct place but it is returning 404 error.

If you need anything else, you can look at my code

Note: I think that django isn't able to read this files but I don't know why... Collectstatic puts all static_cdn/ into static/ in same app dir.

Author:12944qwerty,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57249866/i-keep-getting-a-404-error-when-django-is-looking-for-static-files
Amit :

You probably have set DEBUG to False. If and only if, you are in development environment , set DEBUG to True. If you are planning for production, do collectstatic first and do appropriate changes in the settings.py and set DEBUG back to False. See the Django doc and your production server doc. Also don't forget to restart the server.\nBest wishes.",
2019-07-29T09:13:31
Shirish Goyal :

In your code, you have defined BASE_DIR as below\n\nBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\n\nwhich means it is pointing to the root folder while your static_cdn folder is lying under business folder\n\nWhile defining below, you will have to update path to one within business folder for STATIC_ROOT and STATICFILES_DIRS\n\nSTATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn')\nSTATIC_URL = '/static_cdn/'\n\nSTATICFILES_DIRS = [\n os.path.join(BASE_DIR, 'static_cdn'),\n]\n\n\nas below\n\nSTATIC_ROOT = os.path.join(BASE_DIR, 'business', 'static')\nSTATIC_URL = '/static_cdn/'\n\nSTATICFILES_DIRS = [\n os.path.join(BASE_DIR, 'business', 'static_cdn'),\n]\n",
2019-07-29T08:40:06
yy