We all know not to serve static media (images, CSS, Javascript, etc.) in production directly from Django. Thankfully, Django gives us some nice settings like MEDIA_URL and MEDIA_ROOT to make serving them a lot less painless. Lately, however, I’ve come to realize that these settings shouldn’t really apply to all static media.
Not All Static Media Is Created Equal
Static media really comes in two flavors. Media that is part of your site like your stylesheets and user generated media or files that are uploaded to the site once it is live. We don’t want to serve all this media from the same place for a couple of reasons:
- Our source checkouts shouldn’t be crufted up by stuff our users are uploading
- User generated content and source code should live in two different places on the filesystem
We could fix the first problem with some .gitignore, but that doesn’t solve the filesystem issue.
Solution: Segregate Your Static Files
The answer is to segregate your static files. Django already does this with ADMIN_MEDIA_PREFIX. We use two additional settings in our Django projects, STATIC_URL and STATIC_ROOT. This lets us put user generated content in MEDIA_ROOT outside our project while still serving STATIC_ROOT from inside our source repo.
Implementation
We use Nginx as a front-end proxy to most of our sites and add this stanza to serve all our static media:
Then we add three directories to <tt>/var/www/domainname.com/</tt>: <ul> <li><tt>admin</tt>, a symlink to <tt>django/contrib/admin/media</tt></li> <li><tt>static</tt>, a symlink pointing inside our project repo respectively</li> <li><tt>media</tt>, a new folder that stores all the user generated content.</li> </ul> Our project settings file looks like this now: and we slap the following code into our urls.py to serve media during development: This has worked very well for us, how do you tackle the issue (or is it not an issue to you)?