Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Toolkit to simplify static media development and deployment.
Python Other

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


One of the more significant development roadblocks we have relates to local vs. deployed media. Ideally all media (graphics, css, scripts) development would occur locally and not use S3. Then, when ready to deploy, the media should be pushed to S3. That way there can be significant changes to media without disturbing the production web site.

To make this easier, I wrote some additions to the sunlightcore module that handles a lot of media tasks. The goal is to develop locally and then flip a switch in production that makes all the media URLs point to S3 instead of the local media directory. Specifically, MEDIASYNC executes the following tasks:

  • run gzip+jsmin on javascript files

  • run gzip+cssmin on css files

  • add expires headers to everything

One flaw is that the MD5 of a compressed file changes each time it is compressed. This will cause css and js files to be reuploaded each time. Images and other files that are not compressed will only be uploaded when they are created or if they have changed.

django-mediasync is a project of Sunlight Foundation (c) 2009. Writen by Jeremy Carbaugh

All code is under a BSD-style license, see LICENSE for details.



python >= 2.4

django >= 1.0

boto >= 1.8d


To install run

python install

which will install the application into python's site-packages directory.

Quick Setup



Additionally, replace the existing MEDIA_URL setting with:

MEDIA_URL = '/media/'

MEDIA_URL is the URL that will be used in debug mode. Otherwise, the MEDIA_URL will be inferred from the settings listed below.

The following settings must also be added:

import os  
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))  

MEDIASYNC_AWS_KEY = "s3_key"  
MEDIASYNC_AWS_SECRET = "s3_secret"  
MEDIASYNC_AWS_BUCKET = "bucket_name"  

Optionally you may specify a key prefix:

MEDIASYNC_AWS_PREFIX = "key_prefix"  

Assuming a correct DNS CNAME entry, setting MEDIASYNC_AWS_BUCKET to and MEDIASYNC_AWS_PREFIX to myproject/media would sync the media directory to

By default, all files are given an expires header of 365 days after the file was synced to S3. You may override this value by adding MEDIASYNC_EXPIRATION_DAYS to

MEDIASYNC_EXPIRATION_DAYS = 365 * 10    # expire in 10 years

Amazon allows users to create DNS CNAME entries to map custom domain names to an AWS bucket. MEDIASYNC can be configured to use the bucket as the media URL by setting MEDIASYNC_BUCKET_CNAME to True.


Previous versions of mediasync rewrote URLs in CSS files to use the correct MEDIA_URL. Now users are encouraged to use relative paths in their CSS so that URL rewriting is not necessary. URL rewriting can be enabled by setting MEDIASYNC_REWRITE_CSS to True.


The media URL is selected based on the DEBUG attribute in When True, media will be served locally instead of from S3. Sometimes it is necessary to serve media from S3 even when DEBUG is True. To force remote serving of media, set MEDIASYNC_SERVE_REMOTE to True.


A static media URL needs to be setup in that enables access to the media directory ONLY IN DEBUG MODE.

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

Directory structure

Create a media directory under the root of the project. Create images, scripts, and styles directories beneath media.


Template Tags

When referring to media in HTML templates you can use custom template tags. These tags can by accessed by loading the media template tag collection.

{% load media %}


Renders the MEDIA_URL from with trailing slashes removed.

<img src="{% media_url %}/images/stuff.png">


Renders a script tag with the correct include.

{% js "myfile.js" %}


Renders a tag to include the stylesheet. It takes an optional second parameter for the media attribute; the default media is "screen, projector".

{% css "myfile.css" %}  
{% css "myfile.css" "screen" %}  


Shortcut to render as a print stylesheet.

{% css_print "myfile.css" %}

which is equivalent to

{% css "myfile.css" "print" %}

css_ie, css_ie6, css_ie7

link elements with conditional statements.

{% css_ie "myfile.css" %}  
{% css_ie6 "myfile.css" %}  
{% css_ie7 "myfile.css" %}  

Writing Style Sheets

Users are encouraged to write stylesheets using relative URLS. The media directory is synced with S3 as is, so relative local paths will still work when pushed remotely.

background: url(../images/arrow_left.png);

Deprecated URL rewriting

Previous versions of mediasync rewrote absolute paths to use the correct MEDIA_URL. If you would prefer to use this old method, write URLs using absolute paths and set MEDIASYNC_REWRITE_CSS = True in

background: url(/media/images/arrow_left.png);

When pushed to S3, the local URL is rewritten as the MEDIA_URL from If the MEDIA_URL is then the CSS rule will be rewritten as:

background: url(;

Joined files

When serving media in production, it is beneficial to combine JavaScript and CSS into single files. This reduces the number of connections the browser needs to make to the web server. Fewer connections can dramatically decrease page load times and reduce the server-side load.

Joined files are specified in using the MEDIASYNC_JOINED. This is a dict that maps individual media to an alias for the joined files.

    'joined.css': ['reset.css','text.css'],
    'joined.js': ['jquery.js','mediasync.js','processing.js'],

Files listed in MEDIASYNC_JOINED will be combined and pushed to S3 with the name of the alias. The individual CSS files will also be pushed to S3. Aliases must end in either .css or .js; mediasync will look for the source files in the appropriate directories based on the alias extension.

The existing template tags may be used to refer to the joined media. Simply use the joined alias as the argument:

{% css_print "joined.css" %}

When served locally, template tags will render an HTML tag for each of the files that make up the joined file:

<link rel="stylesheet" href="/media/styles/reset.css" type="text/css" media="screen, projection" />
<link rel="stylesheet" href="/media/styles/text.css" type="text/css" media="screen, projection" />

When served remotely, one HTML tag will be rendered with the name of the joined file:

<link rel="stylesheet" href="" type="text/css" media="screen, projection" />


./ syncmedia
Something went wrong with that request. Please try again.