开发者

django: admin site not formatted

开发者 https://www.devze.com 2023-03-27 00:27 出处:网络
I have a mostly开发者_开发知识库 entirely plain django project, with no adding of my own media or customization of the admin interface in any way. Running the server with python manage.py runserver re

I have a mostly开发者_开发知识库 entirely plain django project, with no adding of my own media or customization of the admin interface in any way. Running the server with python manage.py runserver results in a nicely-formatted admin interface. Running the server with gunicorn_django does not. Why is this the case, and how can I fix it?

It's definitely an issue of not finding the css files, but where are they stored? I never configured this, and the MEDIA_ROOT setting is ''.

EDIT: I just want to know how django-admin serves the non-existent admin files... and how can I get gunicorn_django to do the same?


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.


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>.


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


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/'


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号