0

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?

Christopher Reid
  • 4,318
  • 3
  • 35
  • 74

1 Answers1

1

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}),
)
toabi
  • 3,986
  • 1
  • 24
  • 24
  • I've been wary of running collectstatic because I don't want all my MEDIA/admin files to be dumped into my STATIC folder, or for the folder structure inside it to be flattened. I'll give it a shot though. – Christopher Reid Apr 16 '14 at 07:48
  • `This will overwrite existing files! Are you sure you want to do this?` Overwriting existing files in my STATIC is exactly what I don't want to do. I guess I can always repopulate the folders manually. But I don't understand how this will help my MEDIA situation as it only appears to be targetting `/home/USERNAME/webapps/static_media` and nothing to do with `.../media_media` – Christopher Reid Apr 16 '14 at 07:51
  • @AllTheTime Ah. Jeah I confused it. Well, for the MEDIA files, can you check the following things: If you run it locally with `manage.py runserver`, are the files reachable? If so: Does your Apache configuration point at the right spot? – toabi Apr 16 '14 at 12:19
  • So... adding your urlpatterns line to my urls magically made everything work! I didn't run collect static though. You say you use this for testing purposes... Is it fine to use in production as well? It gives access to the folder indices but that doesn't seem like a big problem. – Christopher Reid Apr 16 '14 at 20:09
  • @AllTheTime It's more for debugging, because that means that Django serves your static & media files. Usually you don't want that because Apache/Nginx/... can serve them with much more performance. You can just disable the `show_indexes` if you really want to stick to Django serving the files. – toabi Apr 22 '14 at 06:10