Django Admin Page missing CSS
Solution 1
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 runls
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.
Solution 2
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
Solution 3
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
Solution 4
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.
Solution 5
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!
Related videos on Youtube
super9
I work as a developer for a startup in Singapore. Messing around with iOS in my spare time.
Updated on November 03, 2021Comments
-
super9 about 2 years
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.
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
directorydrwxr-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
foldertotal 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 -
super9 over 12 yearsI just navigated to the folder?
-
Nick Dong over 11 yearsI find it, and it works. For django 1.4 with apache, links as follow would be helped.https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/modwsgi/#serving-the-admin-files , http://stackoverflow.com/questions/9967849/django-1-4-admin-static-files-without-staticfiles-app
-
Nick Dong over 11 yearsFirst, I set
STATICFILES_DIRS
as"/usr/lib/python2.6/site-packages/django/contrib/admin/static/"
. and setSTATIC_ROOT
asos.path.join(SETTINGS_PATH, '../static')
and Second I usecollectstatic' to collect static file from 'STATICFILES_DIRS
toSTATIC_ROOT
. Third ,setSTATIC_URL
as/tdjproj/static/
. Forth, Alias STATIC_URL to STATIC_ROOT(eg.Alias /tdjproj/static/ /var/www/vhost1/tdjproj/static/
). -
Alex over 10 yearsPerfect. In my case it was
/usr/lib/pymodules/python2.6/django/contrib/admin/media
. One shouldcd /usr/lib
andls -a -R | grep -i media
to see what the directory is under one's operating system and python version. -
bitcycle over 10 yearsI agree. This seems to be the "proper" way to make these static files available.
-
CoderGuy123 over 8 yearsVery neat quick solution. Worked for me (Python 3.4, Django 1.8 running on Ubuntu 14.10 @ DigitalOcean).