4

Django's stylesheets for it's built-in Admin site aren't being loaded on my production server which runs Django 1.6. I can see that many other people have encountered this problem and I've tried all the solutions that have been provided to them but none work. My Django site runs on top of Gunicorn 19.1.1 behind an Nginx v. 1.2.1 proxy.

Since I'm going to be building a dashboard and will be using the admin site for much more than managing my users, groups, and models, I've created an "admin" app inside my site:

INSTALLED_APPS += ('apps.admin', )

I'm overriding and extending some of Django's admin templates and have copied them from django.contrib.admin.templates here:

/www/site/apps/admin/__init__.py
/www/site/apps/admin/models.py
/www/site/apps/admin/views.py
/www/site/apps/admin/urls.py
/www/example/apps/admin/templates/admin/base.html
/www/example/apps/admin/templates/admin/base_site.html
/www/example/apps/admin/templates/admin/index.html
/www/example/apps/admin/templates/admin/my_dashboard.html # mine

Here are pertinent settings from my urls.py file

from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
    # ...
    url(r'^admin/', include(admin.site.urls)),
    url(r'^admin/dashboard/$',
        'apps.admin.views.dashboard',
        {'template': 'my_dashboard.html'},
        name='dashboard'),
)

Here's my setup. My site's actual static files reside here:

/www/example/static/css
/www/example/static/js

Here's my static root. The Django docs say it should be different than where your static files are actually located.

STATIC_ROOT = "/var/www/example/static/"

The prefix for my static URL:

STATIC_URL = '/static/'

Locations for static files and the finders:

STATICFILES_DIRS = (
    '/www/example/static'
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

My Nginx configuration file:

upstream gunicorn {
    server 127.0.0.1:8000 fail_timeout=0;
}
server {
    listen 80;
    server_name www.example.com;
    rewrite ^/(.*) http://example.com/$1 permanent;
}
server {
    listen 80;
    server_name example.com;
    root /www/example;
    client_max_body_size 4G;
    keepalive_timeout 5;
    location /static/ {
        alias /www/example/static/;
    }
    location /media/ {
        alias /var/www/example/media/;
    }
    try_files $uri @django;
    location @django {
        proxy_pass http://gunicorn;
        proxy_redirect off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /www/example/static;
    }
}

I can see that the site is server my static files correctly. For example I can see this image:

http://example.com/static/img/small_img.jpg

But Nginx raises a 404 error when I try to access a Django stylesheet...

http://example.com/static/admin/css/base.css

... even though doing View Source in my browser shows these links:

<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/static/admin/css/login.css" />

Here are other things I've done:

  • I confirmed that "django.contrib.staticfiles" and "django.contrib.admin" are in INSTALLED_APPS.
  • I went in to the Python shell and verified my STATIC_ROOT, STATIC_URL, and STATICFILES_DIR settings.
  • I ran the "collectstatic" command on my server and verified that it copied the Django admin site's static files to my STATIC_ROOT.
  • I tried adding an additional location directive to my nginx config file as described here and restarted my Nginx service.
  • I tried creating a directory /www/example/static/admin directory and then creating a "static" symlink in it that points to the static directory .../django/contrib/admin/static/ in my virtual environment directory.
  • I loaded the admin homepage in Chrome with the Developer Console visible to see if there was something strange occuring but it only reported the 404 errors.

I'm going to try "monkey patching" things by copying Django's static files to my site's static files directory and changing the hrefs in my version of base.html. However, this isn't a good solution.

Sorry for such a long post. Can anyone tell me what I'm doing wrong? Other than this, the Admin site appears to be working. It's just the stylesheets that don't load. Thanks!

Community
  • 1
  • 1
Jim
  • 13,430
  • 26
  • 104
  • 155
  • Could it be a permissions issue on ```/www/example/static/```? Edit: It looks like your ```STATIC_ROOT``` doesn't match the nginx's ```location /static/``` alias. You're missing ```/var/``` – schillingt Feb 12 '15 at 19:00
  • That was it! I just needed an extra set of eyes. If you'd like to repost your comment as an answer, I'd be very happy to give you credit. Hopefully other people will at least find this useful as a set of steps they can take to troubleshoot this kind of problem. Thanks again. – Jim Feb 12 '15 at 20:29

1 Answers1

4

It looks like your STATIC_ROOT doesn't match the nginx's location /static/ alias. You're missing /var/

schillingt
  • 13,493
  • 2
  • 32
  • 34