5

I have deployed my django website onto webfaction hosting service and i am struggling to find how i can serve user uploaded media files in production. There are lot of question regarding how to serve media files in development, but there seems to be nothing about serving media (user uploaded ) files in production.

At present, my django app looks like below in production.

django_project
|
 ---> media
 ---> static (these are served through collectstatic and no problem with this)
 ---> appname1
 ---> appname2
 --->project_name
 |
     ---> settings.py

And my

MEDIA_ROOT = /django_project/media/

MEDIA_URL = www.website.com/media/

There are lot of user uploaded images that are stored in this media folder. Now when i open website, none of the images are loading up. Can someone help how i can serve media files in production.

Thanks

Sreekanth

Dev
  • 1,529
  • 4
  • 24
  • 36

1 Answers1

1

You need to use django.views.static.serve, something like this:

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

https://docs.djangoproject.com/en/dev/howto/static-files/#serving-other-directories

scoopseven
  • 1,767
  • 3
  • 18
  • 27
  • 1
    Hi, As per the link that line needs to be added if we are using debug and in development. Can we add it in production site as well ? – Dev Mar 29 '13 at 17:26
  • 1
    Sorry, you're right about the not-production ready. We're actually using a couple of other methods there. 1) S3BotoStorage from django-storages (http://django-storages.readthedocs.org/en/latest/index.html), which puts our user uploaded files on S3 when they're uploaded to our server. We're also using an alias in Apache, Alias /media/ /srv/path-to-media/media/ – scoopseven Apr 01 '13 at 01:42
  • Thanks. I will try S3 using django-storages. – Dev Apr 01 '13 at 10:32
  • Is this correct for production? Because this is implementing Django to serve the media. I would expect a server to serve the media, not Django, just like static files do. – Jorge Leitao Aug 23 '13 at 08:40