1

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?

ATang
  • 53
  • 2
  • 7
  • Are you serving the static folder at all via nginx/apache etc... ? – Jon Clements Dec 02 '18 at 19:32
  • 1
    The built-in webserver is for development only. And that is why it does not serve static files when DEBUG is off. – Klaus D. Dec 02 '18 at 19:36
  • Because when `DEBUG` set to `False` django stops serving your static files for you. It will be fine in production with just a few lines in server config. – Nikita Tonkoskur Dec 02 '18 at 19:37
  • @JonClements I'm not serving the static folder through a HTTP Web Server. – ATang Dec 02 '18 at 19:46
  • @AlvinTang well Django when not in debug mode doesn't do it for you as others have mentioned... – Jon Clements Dec 02 '18 at 19:47
  • @AlvinTang have a read of: https://docs.djangoproject.com/en/2.1/howto/static-files/ and https://docs.djangoproject.com/en/2.1/howto/static-files/deployment/ – Jon Clements Dec 02 '18 at 19:48
  • When `DEBUG=False` Django will not serve your static files, that's why it shows 404 files – Lemayzeur Dec 02 '18 at 19:48
  • @KlausD. I'm aware of that, but yet my other projects has had their static files shown. I want to deploy this project on Azure but it doesn't seem to work because of this supposed bug. This bug affects both static files in my local and on App Service on Azure. – ATang Dec 02 '18 at 19:49
  • @Alvin doesn't seem to be a bug... have you checked your other apps aren't running in debug mode or otherwise having their static files served by a front-end server? – Jon Clements Dec 02 '18 at 19:50
  • @JonClements all my other projects on my local machine work fine with their static files when debug if turned off, and I am sure that all my apps in my project have debug mode off as well, because I never set it. – ATang Dec 02 '18 at 19:54
  • @JonClements I have skimmed through them, but I'll read them more thoroughly. – ATang Dec 02 '18 at 19:55
  • @JonClements that's weird, I used WhiteNoise to mimic serving static files on an HTTP web server and it worked, but my other projects have their other static files served without it. I'll look more into it. Thanks for your help and everyone else's help. – ATang Dec 02 '18 at 20:22

2 Answers2

2

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.

Here's another post which proves this:
Why does DEBUG=False setting make my django Static Files Access fail?

Here's a solution to serve static files based off where your hosting your code:

Learn more about Django static files deployment (official documentation) here: Deploying static files

ATang
  • 53
  • 2
  • 7
0

Just to add something @Alvin Tang, For the purpose of testing your project in production when DEBUG = False you may run the server using: python manage.py runserver --insecure . This assumes a False Debug settings, imitates a production environment and deploys your project serving your static files just as expected (or at least this is what I think it does) provided your static settings are correct - which is true in most cases. Tested and working on Django == 3.0.6