Django Admin Page missing CSS

2019-01-08 22:50发布

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

17条回答
smile是对你的礼貌
2楼-- · 2019-01-08 23:19

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
查看更多
混吃等死
3楼-- · 2019-01-08 23:19

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/
查看更多
放荡不羁爱自由
4楼-- · 2019-01-08 23:19

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

查看更多
我只想做你的唯一
5楼-- · 2019-01-08 23:20

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>
查看更多
劫难
6楼-- · 2019-01-08 23:22

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 ?

查看更多
狗以群分
7楼-- · 2019-01-08 23:23

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!

查看更多
登录 后发表回答