Solving admin CSS not loaded or not found in Django


When running Django with development server, open “/admin” will show perfectly without any problem. But, running Django with Apache + ModWSGI or NGINX + UWSGI, CSS admin not loaded and not found. So, Django admin showing plain without CSS. This problem is commonly happen when we deploy Django on production. Why this is happen ?

Note that the Django development server automagically serves the static files of the admin app, but this is not the case when you use any other server arrangement. You’re responsible for setting up Apache, or whichever media server you’re using, to serve the admin files.

The admin files live in (django/contrib/admin/static/admin) of the Django distribution.

We strongly recommend using django.contrib.staticfiles to handle the admin files, but here are two other approaches:

1. Create a symbolic link to the admin static files from within your document root.
2. Or, copy the admin static files so that they live within your Apache document root.

Ref : https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/.

First, let me know you commonly settings.py that manage static and media files for Django :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# CUSTOMIZATION HERE
import os
# This dynamically discovers the path to the project
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = os.path.join(PROJECT_PATH, ‘media’)

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ‘/media/’

# Absolute path to the directory static files should be collected to.
# Don’t put anything in this directory yourself; store your static files
# in apps’ "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(PROJECT_PATH, ‘static’)

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = ‘/static/’

Did you have same configuration with this ? If yes, now you should check if admin static files already collected in static folder (under project, not apps).

If you don’t, then you should generate collecting default admin CSS, Images, JS and several files from Django by :

1
python manage.py collectstatic

This command will copy default admin into “static” folder in project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
You have requested to collect static files at the destination
location as specified in your settings file.

This will overwrite existing files.
Are you sure you want to do this?

Type ‘yes’ to continue, or ‘no’ to cancel: yes
Copying ‘/home/ubuntu/htdocs/django_crawl/posts/static/bootstrap.min.css’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/css/widgets.css’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/css/changelists.css’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/css/ie.css’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/css/rtl.css’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/css/login.css’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/css/dashboard.css’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/css/forms.css’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/css/base.css’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/changelist-bg.gif’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/tool-right_over.gif’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/selector-removeall.gif’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/default-bg.gif’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/default-bg-reverse.gif’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/tooltag-add.gif’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/inline-delete.png’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/chooser-bg.gif’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/icon-yes.gif’
Copying ‘/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/img/admin/arrow-down.gif’
…..

Still style django admin not loaded properly and not found in ‘/static’. So what else ?

Check your Apache virtualhost. For instance, this is mine :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<VirtualHost *:80>
    ServerAdmin ubuntu@django_crawl
    ServerName django_crawl
    DocumentRoot /var/www/django_crawl/

    # Django settings
    WSGIScriptAlias / /var/www/django_crawl/django.wsgi
    WSGIDaemonProcess django_crawl user=www-data group=www-data processes=1 threads=10
    WSGIProcessGroup django_crawl

    # Non-Django directories
    Alias /static /var/www/django_crawl/posts/static/
    <Location "/static">
        SetHandler None
    </Location>

    <Directory /var/yodi/django_crawl/posts>
      Order allow,deny
      Allow from all
    </Directory>

    Alias /robots.txt /var/www/django_crawl/posts/robots.txt
    Alias /favicon.ico /var/www/django_crawl/posts/favicon.ico
   
    ErrorLog /var/log/apache2/error.log
    CustomLog /var/log/apache2/access.log combined

</VirtualHost>

If we have an django projects, then we should point to project folder, not APPS folder. So, the right Virtualhost is :

The right configuration virtualhost for Apache + ModWSGI run Django


That Virtualhost is totally WRONG!

Now you have Django admin run perfectly! 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.