mobina sattari
mobina sattari

Reputation: 51

why django admin static files doesn't load

Since I had started Django, I'm tackling some ridiculous problems. Recently, when I start a new project and I run it on the server, Django's admin CSS is not loaded. The last project I ran on the server, after a while it was okay, and the real Django admin template was there and the CSS was loaded and it was working. But this time again the same problem happened and I don't know how to solve it cause the project here is the photo is raw, with no special code. I don't know if it's Chrome problem or not, but I have tried it on other browsers and it was the same thing.

I would be glad if you can help me

Upvotes: 2

Views: 6286

Answers (3)

Tony
Tony

Reputation: 1

If you are running Django + gunicorn with NGINX or Caddy and you are sure your configurations are correct, one thing to check is that you are connecting to NGINX / Caddy and not gunicorn itself.

i.e. If you are connected to localhost:8000/admin, you are connected to gunicorn and you will not get CSS / static files. Connect instead to localhost/admin

Coming back to a django project after a few months away and I was bashing my head against this for hours.

Upvotes: 0

mobina sattari
mobina sattari

Reputation: 51

the answer I found was to change STATIC_URL = 'static/' in settings.py to STATIC_URL = '/static/'. Only one '/' may change your whole admin appearance. This problem may not happens to everyone but running code in Pycharm, I had been tackling it for such a long time.

Upvotes: 3

Khan Asfi Reza
Khan Asfi Reza

Reputation: 691

If this problem is happening locally, then just change your settings.py

DEBUG = True
#...
#...
STATIC_ROOT = YOUR_STATIC_ROOT_DIRECTORY

Then run collectstatic, make sure you have a proper STATIC_ROOT directory which is also in your settings.py file

python manage.py collectstatic

If you have deployed your app on a production server then, you have to follow certain things in order to get stylesheet and javascript files.

Firstly In your urls.py you need to add this code

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
               static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Then you have to use a third-party media file server(CDN) Like AWS S3 or you can serve your css, js or media files from your Django server If you don't want to use AWS S3, then you can either send css/js using your django app (which is not ideal to do), or you can use nginx or apachee to send your css/js

For Nginx to send js/css You have to add a Static and Media File Root that can be accessed by your django application, I usually use /var/www/ to serve static and media from any Linux server

STATIC_ROOT = '/var/www/static'
MEDIA_ROOT = 'var/www/media'

Then if you are using nginx

server {
    server_name domainname.com;

    access_log off;

    location /static/ {
        root /var/www/static;
    }
    location /media/ {
        root /var/www/media;
    }

If it is still not working, then your django app might not be able to use the given static_root and media directory, make sure your app has access to them

If you want to send your js/css from your django app (Better not to do in production) Then

To install whitenoise

pip install whitenoise

In your settings file

MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

Upvotes: 1

Related Questions