Home > Blockchain >  Django - CSS File Not Loading In Production server (Debug: False)
Django - CSS File Not Loading In Production server (Debug: False)

Time:10-21

Use : nginx,gunicorn,linode for server

Debug=False

When I keep debug=False in production the css file don't get loaded.I have also build 404.html page.Suppose some one visit mydomain.com/abcd then he/she will get the 404 page which I have designed.Its good.The issue is css file not loaded.

Debug True

When I keep debug=True in the production the css file get loaded.Everything goes right.But when someone visited the mydomain.com/abcd then he/she will get the django defaualt error page.If I keep debug=True in the production everything goes right but I have heard that keeping debug=True in production is not recommended and may cause the security issues in the sites

Currently what I have in my settings.py and nginx cofig are :

settings.py :

DEBUG =True

ALLOWED_HOSTS = ['ip','mydomain.com','www.mydomain.com']

Nginx config file :

server {
    server_name mydomain.com www.mydomain.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location projectdir/static/ {
        autoindex on ;
        root /home/user/projectdir;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

Please help me to solve the issue,since I am struggling from past 1 week. Currently the site is live and css is loaded by keeping debug=True.But I don't want to have any security issue later.

CodePudding user response:

In settings.py file:

You can simply add like below code:

DEBUG = bool(int(os.environ.get('DEBUG', 0))) #Note: 0 means false and 1 means true

Try this way and check if this is solves your error

CodePudding user response:

config required in your settings.py

...
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"

make sure you've run the following, so django finds all your static files and puts them in one place

$python manage.py collectstatic 

nginx static config should look more like

...
location /static/ {
    autoindex on;
    alias /home/user/projectdir/static/;
}
  • Related