Why Does My Django Admin Site Not Have Styles/CSS Loading

Why does my Django admin site not have styles / CSS loading?

Django does not serve static files on it's own. You have to tell it where the files are.

The ADMIN_MEDIA_PREFIX in the settings.py will point Django in the right location.

Since you're using the development version, you'll want the dev-specific document for static files how-to. Adam's link will lead you to the 1.2 version.

CSS is not loading in Django Admin Panel App?

Thanks, Guys I added the following in my setting.py file, and now it's working fine:

import mimetypes
mimetypes.add_type("text/css", ".css", True)

Django admin site not showing CSS style

Your Apache configuration requires that all static content (CSS, JS, images etc) should be in the mysite/static directory. You should collect your static content from apps into the mysite/static directory first:

cd /home/ubuntu/cs462/mysite
python manage.py collectstatic

Update: If you did not specify a location for static content, you should add the following lines to your settings.py:

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

Django Admin loading without css

The problem was that when I was using Pythonanywhere, I wasn't using python manage.py runserver command because my server was being hosted by pythonanywhere. I didn't know that with DEBUG=TRUE, the runserver command actually serves static files. What I ended up doing was setting my static_root, static_url, and staticfiles_dirs to the correct values in settings.py and then running python manage.py collectstatic to compile all my static files into the right folder.

EDIT

I realize that my answer needs elaboration, so I am describing what I ended up setting my settings to.

For my STATIC_ROOT, I ended up setting it to BASE_DIR / "cdn_test" / "static". I did this because when you deploy your project, you will end up hosting your static files on a server(a cdn) that will host and serve your static files*. In production, you will also store your media (images) and more in your cdn folder.

For my STATIC_URL, I just set it to /static/. You can read more about it in the Django docs.

Finally, for my STATICFILES_DIRS, I set it to

STATICFILES_DIRS = [
BASE_DIR / "static",
]`.

This is because Django copies the files from this static folder into the cdn_test/static folder. Whatever is in the static folder is what you edit; the cdn_test. If you have any other folders where you have static files, you can add them to your STATICFILES_DIRS.

Now, whenever you make changes to your static files, you will run python manage.py collectstatic. This is not like development, where Django automatically serves your static files for you.

I hope all this makes sense because it gave me a fair bit of confusion initially when I was learning.

*Note that you have to create the cdn_test and the static dir inside it by yourself.

Django admin interface missing css styling in production

Could you please try below steps and let me know if it's working or not?

Apply below changes in settings.py file:

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

Remove below line from your settings.py:

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]

Execute below command in production:

python manage.py collectstatic

Update nginx file like below one:

server {
listen 80;
server_name MY_SERVER_IP;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
autoindex on;
autoindex_exact_size off;
root /home/MYUSERNAME/myproject;
}

location /media/ {
autoindex on;
autoindex_exact_size off;
root /home/MYUSERNAME/myproject;
}
}

Explanations:

  • STATIC_ROOT is the folder where static files will be stored after
    using python manage.py collectstatic
  • STATICFILES_DIRS is the list of folders where django will search for additional static files aside from the static folder of each app installed.

In this case our concern was Admin related CSS files that why we use STATIC_ROOT instead of STATICFILES_DIRS

why django admin template doesn't load

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.



Related Topics



Leave a reply



Submit