Home:ALL Converter>Django: Why are my static files not showing up and always giving me a 404 error when DEBUG=False?

Django: Why are my static files not showing up and always giving me a 404 error when DEBUG=False?

Ask Time:2018-12-03T03:30:29         Author:ATang

Json Formatter

I am working on my Windows Machine, trying to develop multiple apps for a project called "portal". After working on it, I have set DEBUG=False, and now all my static files are giving me a 404 error after loading any page. When running python manage.py runserver in CMD, I get this when loading a page:

[02/Dec/2018 14:10:14] "GET /account/sign-in HTTP/1.1" 200 6249
[02/Dec/2018 14:10:14] "GET /static/fonts/fonts.css HTTP/1.1" 404 96
[02/Dec/2018 14:10:14] "GET /static/css/argon.css HTTP/1.1" 404 94
[02/Dec/2018 14:10:14] "GET /static/branding/logo.png HTTP/1.1" 404 98

I have looked at over 20+ posts about this which were mostly the same and I have followed all of their steps, such as:

  • I have set these in my settings.py file: STATIC_URL = '/static/', STATICFILES_DIRS = ['portal/static/'] (I have a static folder in the folder that holds files like settings.py), and STATIC_ROOT = os.path.join(BASE_DIR, "static")
  • I have called python manage.py collectstatic

I have even created a new Django test project and did all these steps above, and they ARE working for that new test project. I have even removed all files in __pycache__ and reset my migrations, and database files.

Are there any other possible secure (I have seen others use cheats such as --insecure) solutions to fix my project so it can go into production other than the other solutions above?

Author:ATang,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/53583826/django-why-are-my-static-files-not-showing-up-and-always-giving-me-a-404-error
ATang :

So I looked more into what Jon Clements and the others said about how Django won't handle static files anymore when DEBUG=False, and that you need an HTTP Web Server to serve those files. That's actually true. I never thought that before because my other projects had their static files still served, but it could have been because my browser cached those files.\n\nHere's another post which proves this:\nWhy does DEBUG=False setting make my django Static Files Access fail?\n\nHere's a solution to serve static files based off where your hosting your code:\n\n\nWithout a HTTP Web Server, you can serve your static files using WhiteNoise (but I wouldn't recommend it for production)\nWith Apache or any other HTTP Web Server, you can serve your static files by modifying your config file (learn more here: How to use Django with Apache and mod_wsgi)\nIf you are using Azure, like I am, I would recommend using Blob storage to handle all static and media files (Medium post: Django — Using Azure blob storage to handle static & media assets — from scratch)\n\n\nLearn more about Django static files deployment (official documentation) here: Deploying static files",
2018-12-03T17:00:43
yy