Django: Serving Media Files During Development Process

2009 Sep 06 - Brian Kloppenborg

While developing in Django, it is often useful to serve media/static files to ensure design templates are displayed correctly. In order to do so, you need to add a custom line to the file:

    # ...
    if settings.DEBUG:
        urlpatterns += patterns('',
             {'document_root': '/path/to/media'}),

In addition to this modification, I also like to enable directory listings by modifying the file as follows:

    from django.conf.urls.defaults import *
    from django.conf import settings    
    # Uncomment the next two lines to enable the admin:
    from django.contrib import admin
    urlpatterns = patterns('',
        # Uncomment the next line to enable the admin:
        (r'^admin/', include(,
    if settings.DEBUG:
        urlpatterns += patterns('',
            (r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:],
             {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),

Now, I wanted all of my media files served from the /media directory on my webserver, unfortunately the admin interface handles all incoming requests to /media by default. Therefore, I moved the admin directory in my file and cleaned things up a little so I can quickly deploy my website:

       # Settings on Development Server
        MEDIA_ROOT = '/path/to/development/media/'
        MEDIA_ROOT = '/path/to/deployed/media/'
    MEDIA_URL = '/media/'
    ADMIN_MEDIA_PREFIX = '/media/admin/'</pre>

Further information on serving static content with the development server can be found on the Django Static File Documentation page.