views:

463

answers:

2

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.

+4  A: 

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.

Frozenskys
My mistake was that `ADMIN_MEDIA_PREFIX` was set to be `/media/` as well, which caused things not to work.
cool-RR
+5  A: 

Straight from the comments in settings.py...

MEDIA_ROOT

The MEDIA_ROOT is the absolute path to the directory that holds media such as /home/media/media.lawrence.com/.

MEDIA_URL

The MEDIA_URL is the URL that handles the media served from MEDIA_ROOT. Make sure to use a trailing slash if there is a path component (optional in other cases). Examples: "http://media.lawrence.com", "http://example.com/media/".

So, to reword those... The MEDIA_ROOT is where the files live physically on your system, and the MEDIA_URL is where those files are mapped to. In development, this might not always be accessible, and in most cases your dev environment and your production environment are not the same, and it is something you're going to have to go back and change. The other thing is that it is NOT A GOOD PRACTICE when Django was designed NOT to serve static content for you.

If you're going to use this in development, I suggest you use the method of limiting it to DEBUG=True. Telling Django to serve static content from a temporary location while in development when the DEBUG is set to True is a much better and safer practice. You're NOT going to put your site into production with DEBUG on, right? Well, at least you shouldn't.

Here is how I implemented it:

settings.py:

STATIC_DOC_ROOT = os.path.join(os.getcwd(), 'site_media')

urls.py:

from django.conf import settings
## debug stuff to serve static media
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', 
            {'document_root': settings.STATIC_DOC_ROOT}),
   )

This way any project I'm working on has a site_media directory inside of it with all of the media necessary. In dev it is self-contained and I don't have to flip any bits in the settings except for DEBUG, which I would be doing anyways.

jathanism
+1 for pointing out the `DEBUG` trick. I use it always.
ayaz
Debug should not be necessary as any requests to the URL should never hit Django and should be served by the front end web server in a production setting ;)
Frozenskys