[Django]-Django: admin site not formatted

1đź‘Ť

If you use contrib.static, you have to execute a collectstatic command to get all the app-specific static files (including admin’s own) into the public directory that is served by gunicorn.

👤rewritten

1đź‘Ť

I’ve run into this problem too (because I do some development against gunicorn), and here’s how to remove the admin-media magic and serve admin media like any other media through urls.py:

import os

import django

...

admin_media_url = settings.ADMIN_MEDIA_PREFIX.lstrip('/') + '(?P<path>.*)$'
admin_media_path = os.path.join(django.__path__[0], 'contrib', 'admin', 'media')

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^' + admin_media_url , 'django.views.static.serve', {
        'document_root': admin_media_path,
    }, name='admin-media'),
    ...
)

Also: http://djangosnippets.org/snippets/2547/

And, of course, #include <production_disclaimer.h>.

👤David Wolever

1đź‘Ť

I think the easiest way is to add alias to nginx (are you using one?!) configuration file:

location /static/admin/ {
    alias /<path_to_your_admin_static_files>/;
}

it worked immediately for me

👤Slaven Tomac

0đź‘Ť

Ok, got it. Just had to add this line to settings.py:

MEDIA_ROOT = '/home/claudiu/server/.virtualenv/lib/python2.5/site-packages/django/contrib/admin/media/'
👤Claudiu

0đź‘Ť

David Wolever’s answer was close, for my installation, but I think some paths may have changed in newer django. In particular I set

admin_media_path = os.path.join(django.__path__[0], 'contrib', 'admin', 'static', 'admin')

and in urlpatterns added:

url(r'^static/admin/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': admin_media_path,
    }),

based on info found here: https://docs.djangoproject.com/en/dev/howto/static-files/

works for me, but more “magical” than I like.

👤Brent Noorda

Leave a comment