Django Admin Page missing CSS

Question:

I saw this question and recommendation from Django Projects here but still can’t get this to work. My Django Admin pages are not displaying the CSS at all.

enter image description here

This is my current configuration.

settings.py

ADMIN_MEDIA_PREFIX = '/media/admin/'

httpd.conf

<VirtualHost *:80>
    DocumentRoot /home/django/sgel
    ServerName ec2-***-**-***-***.ap-**********-1.compute.amazonaws.com
    ErrorLog /home/django/sgel/logs/apache_error.log
    CustomLog /home/django/sgel/logs/apache_access.log combined
    WSGIScriptAlias / /home/django/sgel/apache/django.wsgi

    <Directory /home/django/sgel/media>
        Order deny,allow
        Allow from all
    </Directory>

    <Directory /home/django/sgel/apache>
        Order deny,allow
        Allow from all
    </Directory>

    LogLevel warn

    Alias /media/ /home/django/sgel/media/

</VirtualHost>

<VirtualHost *:80>
   ServerName sgel.com
   Redirect permanent / http://www.sgel.com/
</VirtualHost>

In addition, I also ran the following to create (I think) the symbolic link
ln -s /home/djangotest/sgel/media/admin/ /usr/lib/python2.6/site-packages/django/contrib/admin/media/

UPDATE

In my httpd.conf file,

User django
Group django

When I run ls -l in my /media directory

drwxr-xr-x 2 root root 4096 Apr  4 11:03 admin
-rw-r--r-- 1 root root    9 Apr  8 09:02 test.txt

Should that root user be django instead?

UPDATE 2
When I enter ls -la in my /media/admin folder

total 12
drwxr-xr-x 2 root root 4096 Apr 13 03:33 .
drwxr-xr-x 3 root root 4096 Apr  8 09:02 ..
lrwxrwxrwx 1 root root   60 Apr 13 03:33 media -> /usr/lib/python2.6/site-packages/django/contrib/admin/media/

The thing is, when I navigate to /usr/lib/python2.6/site-packages/django/contrib/admin/media/, the folder was empty. So I copied the CSS, IMG and JS folders from my Django installation into /usr/lib/python2.6/site-packages/django/contrib/admin/media/ and it still didn’t work

Asked By: super9

||

Answers:

There’s a couple of problems here, both to do with your symbolic link.

Firstly, the source and target needed to be the other way round (I always get that wrong myself).

Secondly, you have used a completely different path to the one you’ve specified in your Apache conf – djangotest/sgelections vs django/sgel.

Do it like this:

cd /home/django/sgel/media/
ln -s /usr/lib/python2.6/site-packages/django/contrib/admin/media/ admin
Answered By: Daniel Roseman

can you run

python
>>> import django
>>> print django.__file__

the other question – does your normal media placed into the /home/django/sgel/media/ work (i.e. served by Apache as expected?)

Answered By: Guard

In addition to correcting the symbolic link as Daniel Roseman suggested, you’ll need to make sure that the user that is running Apache has read access to the admin media.

  • If you do ls -l in your media directory, do you see the symbolic link?
  • If you cd admin from your media directory, does it work? If you then run ls can you see the admin media?
  • Does the user that runs Apache have read access to the admin media?

If all those things work, then please update your question with your current configuration and results of those commands and we’ll take another look.

Response to Update: Ok, the permissions look ok. It looks like you’ve got the directory structure in your media directory a little bit wrong.

The fact that /usr/lib/python2.6/site-packages/django/contrib/admin/media/ was empty is disturbing, too. Once you solve the immediate problem you may want to look into reinstall django in the expected place.

Anyways, here’s how the structure should look:

$ cd media
$ ls -la
drwxr-xr-x 2 root root 4096 Apr 13 03:33 .
drwxr-xr-x 3 root root 4096 Apr  8 09:02 ..
lrwxrwxrwx 1 root root   60 Apr 13 03:33 admin -> /usr/lib/python2.6/site-packages/django/contrib/admin/media/
-rw-r--r-- 1 root root    9 Apr  8 09:02 test.txt

That is, inside of the media/ directory their should be a link called admin directly to the /admin/media directory of your django installation.

To fix what you’ve got, inside of the media/admin/ directory run:

rm media
cd ..
rmdir admin

and then re-create the symlink as suggested in Daniel Roseman’s answer.

Answered By: Sean W.

Try to add

Options FollowSymLinks

to your

<Directory /home/django/sgel/media>
    Order deny,allow
    Allow from all
</Directory>

so that you end up with

<Directory /home/django/sgel/media>
    Options FollowSymLinks
    Order deny,allow
    Allow from all
</Directory>
Answered By: skrat

I’m not sure if this will help but in my config file I have:

    Alias /adminmedia/ /var/lib/python-support/python2.6/django/contrib/admin/media/   

    <Directory "/var/lib/python-support/python2.6/django/contrib/admin/media"> 
            AllowOverride None                                                 
            Options FollowSymLinks                                             
            Order allow,deny                                                   
            Allow from all                                                     
    </Directory>

It works but I have my server set up for development/testing only.

Answered By: James Khoury

i used to have the same problem, i solved it by using the FireFox plugin firebug, which tells you where is your site looking for the media files, also how did you check the contents of the admin/media folder to see if they were empty ?

Answered By: Paulo

In Firebug, use the Net tab and see the requests being made by your browser for the css files. see the response for your requests there itself. I think you will find the problem there.
I recently had the same problem. The problem was when my browser requests for the css files, the response contained the contents of my project’s login html page. I don’t remember now that how i solved my problem. I will try to recollect and post the solution here.
Let me know if you have the same problem.

Answered By: Arihant Nahata

Did you try setting the value for

MEDIA_ROOT
MEDIA_URL
ADMIN_MEDIA_PREFIX

correctly?

I mean that the MEDIA_ROOT url and ADMIN_MEDIA_PREFX should have a different value. Please check these values in your settings.py and try again.

Hope this helps.

Answered By: Mahendra Liya

I encountered the same problem while running the Bitnami Django Stack on Win32 (builtin development server)

I solved the issue by finding the missing CSS folders in the installation and changing settings.py.

# Additional locations of static files
STATICFILES_DIRS = (
    'C:/Program Files/BitNami DjangoStack/apps/django/django/contrib',
)

This will help you get started developing… It’s not optimal. Don’t forget the trailing comma 🙂

Answered By: Amanjit Gill

Another method I just found, and it looks like a properly supported method.

Make sure you have the staticfiles module in your settings. Also configure the STATIC_ROOT option.

https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#module-django.contrib.staticfiles

Create your /static/ folder and set permissions.

Configure apache with a /static/ alias.

alias /static <path to project root>/static/
<Directory "<path to project root>/static/">
    Order deny,allow
    Allow from all
</Directory>

then from your project folder run

sudo python manage.py collectstatic [-n to do a dry run]

All this looks like it does is copy the admin static files to your projects folder for web serving.

Answered By: niblitz

I know it’s been solved but I think it’s worthy to share my solution.

I simply added the alias in apache and it worked so far.

    Alias /static/admin/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/
    Alias admin/media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/
Answered By: Fabricio Buzeto

If nothing helps, add the following to urls.py

url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { <br/>
            'document_root': '/usr/lib/python2.4/site-packages/django/contrib/admin/media/', <br/>
           'show_indexes' : True, <br/>
        }), <br/>

This is independent of apache or nginx

Answered By: vsnu

when we talked about django, we should know which edition we run on. “ADMIN_MEDIA_PREFIX” is used in django before edition of 1.4, see

The included administration app django.contrib.admin has for a long time 
shipped with a default set of static files such as JavaScript, images and 
stylesheets. Django 1.3 added a new contrib app django.contrib.staticfiles 
to handle such files in a generic way and defined conventions for static 
files included in apps.
Starting in Django 1.4, the admin's static files also follow this convention, 
to make the files easier to deploy. In previous versions of Django, it was 
also common to define an ADMIN_MEDIA_PREFIX setting to point to the URL where 
the admin's static files live on a Web server. This setting has now been deprecated 
and replaced by the more general setting STATIC_URL. Django will now expect to 
find the admin static files under the URL <STATIC_URL>/admin/.

https://docs.djangoproject.com/en/dev/releases/1.4/.
I am using django 1.4, and still confusing about this.

Answered By: Nick Dong

So the first thing you want to do is change directory to your static folder and add a symlink.

I did this with a virtual environment so I use

ln -s ~/virtualenv/my-virtualenv/lib/python2.x/site-packages/django/contrib/admin/static/admin admin

The next step is to edit your httpd.conf

Alias /static/admin/ ~/mysite/static/admin/

Restart your apache server and voila!

Answered By: Domino_KOI

I had the same problem but none of this helped really. Turned out my version of grappelli was outdated. (https://github.com/sehmaschine/django-grappelli) I uninstalled it and then re-installed with a newer version.
pip install django-grappelli==2.4.4
If you are using an admin skin like me, you might consider updating it.

Answered By: jason gotlieb

If you’re using virtualenvwrapper like me, then this is how you can find your admin media files and fix it:

workon <project_name>
export DJANGO_BASEFILE=`python -c 'import django; print django.__file__'`
export DJANGO_BASEDIR=$(dirname ${DJANGO_BASEFILE})
ln -s $DJANGO_BASEDIR/contrib/admin/media <project_dir>/media/admin

Replace the <project_name> to your virtualenvwrapper environment name and project_dir to your working directory. 🙂

Answered By: nkh

The easy solution is to change a line in your wsgi.py file

    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()

becomes

    from django.core.wsgi import get_wsgi_application
    from dj_static import Cling

    application = Cling(get_wsgi_application())

Easiest way to have the Admin CSS show properly. Cheers

Answered By: Adam

In my case, this issue occured after setting up the nginx on Ubuntu server. On analyzing the file structure, I found that admin folder was missing from the staticfile directory (the location where django looks for static files to add to STATIC_ROOT). So, I copied the admin folder (inside static directory) to the server’s static folder.
Then run

python manage.py collectstatic

And now, django collects the 130 files required for admin page styling.

I want to add to Adam’s Solution,

first you need to install dj-static in cpanel. So add dj-static==0.0.6 in requirements.txt then install

dj-static==0.0.6

and then edit the project wsgi.py file:

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

then restart the cpanel. Hope Admin will have a Proper Look. Thanks

I am Ansh Sharma. I also faced this same issue, and it was just too scary. Here I have something which can solve the problem. Go to Your setting file and make

DEBUG = TRUE

I hope this solves your issue
Happy Coding.
Ansh Sharma

Answered By: Ansh Sharma

a simple answer for this problem is moving admin and static folder to public_html

I had this problem and it worked
try it

Answered By: Hamideh Kavoosi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.