简体   繁体   中英

Django - Serving Media/Admin files in production with Apache/Webfaction

As it stands my STATIC files are served without issue using the configuration below.

In my settings.py:

MEDIA_ROOT = '/home/chronic88/webapps/media_media/'
MEDIA_URL = '/media/'

STATIC_ROOT = '/home/chronic88/webapps/static_media/'
STATIC_URL = '/static/'

static_media and media_media are both applications served by apache.

I can upload files via the admin and they show up inside the folder media_media , but they won't display on their pages. When I check the file paths in the page source they seem correct mydomain.com/media/image.png but they simply won't display. So, it seems the link is there but there is some problem communicating between apache and django that I can't put my finger on.

And my main urls.py:

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This is the urls.py I'm using in prodution that works. I tried it without the last line in production but it gives the same result (files are uploaded but not displayable).

What am I missing?

If you configured your Apache to serve the static files from the respective root folders, you also have to run manage.py collectstatic (see docs ).

To test static and media files I usually have this in my urls.py:

urlpatterns += patterns('',
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True}),
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM