Search code examples
djangoapache

Django admin site not showing CSS style


My Django admin website (it's completely default, not customized) is not showing the expected CSS.

It looks like this:

enter image description here

And I can log in:

enter image description here

But it is supposed to look like this:

enter image description here

How do I fix this?

Other info that might help:

I'm running on an Amazon EC2 instance on port 80 and connecting to it using a real URL. I set it up using this tutorial: http://www.nickpolet.com/blog/deploying-django-on-aws/1/

Following that tutorial, I put this code into a file called /etc/apache2/sites-enabled/mysite.conf. I don't understand what this code is doing, so I think it might be related to the problem.

/etc/apache2/sites-enabled/mysite.conf:

WSGIScriptAlias / /home/ubuntu/cs462/mysite/mysite/wsgi.py
WSGIPythonPath /home/ubuntu/cs462/mysite
<Directory /home/ubuntu/cs462/mysite/mysite>
    <Files wsgi.py>
        Order deny,allow
        Require all granted
    </Files>
</Directory>

Alias /media/ /home/ubuntu/cs462/mysite/media/
Alias /static/ /home/ubuntu/cs462/mysite/static/

<Directory /home/ubuntu/cs462/mysite/static>
    Require all granted
</Directory>

<Directory /home/ubuntu/cs462/mysite/media>
    Require all granted
</Directory>

Directory structure:

/home/ubuntu/cs462/
        mysite/
            manage.py
            db.sqlite3
            mysite/
                __init__.py
                __init__.pyc  
                settings.py  
                settings.pyc  
                urls.py  
                wsgi.py
            homepage/
                admin.py  
                admin.pyc  
                __init__.py  
                __init__.pyc  
                migrations  
                models.py  
                models.pyc  
                tests.py  
                views.py

Last part of settings.py:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

Solution

  • Your Apache configuration requires that all static content (CSS, JS, images etc) should be in the mysite/static directory. You should collect your static content from apps into the mysite/static directory first:

    cd /home/ubuntu/cs462/mysite
    python manage.py collectstatic
    

    Update: If you did not specify a location for static content, you should add the following lines to your settings.py:

    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')