[Django]-Django Admin Page missing CSS

13๐Ÿ‘

โœ…

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.

๐Ÿ‘คSean W.

7๐Ÿ‘

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
๐Ÿ‘คDaniel Roseman

6๐Ÿ‘

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

๐Ÿ‘คAdam

5๐Ÿ‘

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.

๐Ÿ‘คniblitz

3๐Ÿ‘

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!

๐Ÿ‘คDomino_KOI

2๐Ÿ‘

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.

๐Ÿ‘คArihant Nahata

2๐Ÿ‘

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.

๐Ÿ‘คjason gotlieb

1๐Ÿ‘

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?)

๐Ÿ‘คGuard

1๐Ÿ‘

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>
๐Ÿ‘คskrat

1๐Ÿ‘

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.

๐Ÿ‘คJames Khoury

1๐Ÿ‘

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 ?

๐Ÿ‘คPaulo

1๐Ÿ‘

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/
๐Ÿ‘คFabricio Buzeto

0๐Ÿ‘

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.

๐Ÿ‘คMahendra Liya

0๐Ÿ‘

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 ๐Ÿ™‚

๐Ÿ‘คAmanjit Gill

0๐Ÿ‘

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

๐Ÿ‘คvsnu

0๐Ÿ‘

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.

๐Ÿ‘คNick Dong

0๐Ÿ‘

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. ๐Ÿ™‚

๐Ÿ‘คnkh

0๐Ÿ‘

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.

0๐Ÿ‘

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

0๐Ÿ‘

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

I had this problem and it worked
try it

๐Ÿ‘คHamideh Kavoosi

-1๐Ÿ‘

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

๐Ÿ‘คAnsh Sharma

Leave a comment