Why can't Django find my multimedia admin files after I leave the embedded server?

When I used the built-in simple server, everything is fine, the admin interface is beautiful:

python manage.py runserver

However, when I try to serve my application using a wsgi server with django.core.handlers.wsgi.WSGIHandler, Django seems to forget where the administrator’s multimedia files are located, and the admin page itself is not written at all:

gunicorn_django

How did this happen?

+3
source share
3 answers

When I look in the Django source code, I will find out the reason.

Somewhere in the module, django.core.management.commands.runserveran object is WSGIHandler wrapped inside AdminMediaHandler.

According to the document AdminMediaHandleris

WSGI, media media,      ADMIN_MEDIA_PREFIX, .      , !      .

, .

URL- URL- :)

+2

Django , ( ..). , , ( ), ( -). django/contrib/admin/media. MEDIA_URL ADMIN_MEDIA_URL, . . http://docs.djangoproject.com/en/dev/howto/static-files/#howto-static-files.

+1

I also ran into this problem (because I am doing some development against the sack), and here's how to remove the magic of admin media and serve administrative tools, like any other media, via 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>.

0
source

Source: https://habr.com/ru/post/1749817/


All Articles