Search code examples
pythondjangobackendwebserver

Why does Django admin page and the rest of my pages' CSS files and images are not found?


the GET method returns 404 in the console even though its searching in the correct file path.

EDIT: i found another post that said i should turn on the DEBUG in the settings.py file. AND IT WORKED!! now after trying to turn it back to False, it still worked as expected. so the second question is why did it not work on the first try?

i made sure the static paths in the settings.py file are correct but it seems the issue is with django itself since the django admin page doesn't show CSS either. i tried python manage.py collectstatic and it didn't work i tried restarting the server multiple times and still

another edit: i just found out that some pages loaded their images and their CSS files while the debug was set to True. but the pages i didn't load, didn't load theirs. after setting the DEBUG to True again and loading the pages, they all now work while the DEBUG is back to False. thought this might help figuring out why does it not work while the DEBUG is set to False


Solution

  • EDIT: i found another post that said i should turn on the DEBUG in the settings.py file. AND IT WORKED!! now after trying to turn it back to False, it still worked as expected. so the second question is why did it not work on the first try?

    It keeps working due to caching. If you clear the cache, with Ctrl + F5, it will again not work with the static files.

    I made sure the static paths in the settings.py file are correct but it seems the issue is with django itself since the django admin page doesn't show CSS either. i tried python manage.py collectstatic and it didn't work i tried restarting the server multiple times and still.

    collectstatic [Django-doc] does not solve this problem. collectstatic just copies the static/ directories of all apps to a general static directory specified by the STATIC_ROOT setting [Django-doc], but it does not serve the files.

    Django does not serve static files in production, it does not make much sense: that would not be efficient. Normally you let Nginx, Apache or another web server that handles requests first.

    For Nginx, it thus looks like:

    server {
        listen 80;
        server_name server_domain_or_IP;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /parent/directory/of/static;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/run/gunicorn.sock;
        }
    }

    so Nginx serves the files, not Django.