Search code examples
pythondjangostatic-media

Serving static media during Django development: Why not MEDIA_ROOT?


I read this guide about serving static media with Django during development.

I noticed that MEDIA_URL and MEDIA_ROOT were not used in this. Why? What's the difference?

I tried doing it with MEDIA_URL and MEDIA_ROOT, and got weird results.


Solution

  • In a production situation you will want your media to be served from your front end web server (Apache, Nginx or the like) to avoid extra load on the Django/Python process. The MEDIA_URL and MEDIA_ROOT are usually used for this.

    Running the built in Development server you will need to set the correct url in your url.py file - I normally use something like this:

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

    Which picks up the MEDIA_ROOT from your settings file meaning that it works for development and live.