Handling Static Files

Introduction

One of the features when migrating to a production environment is that Django gives you the option of splitting your content between dynamically produced content served by the Django’s server and static content that is saved as a file and served separately, potentially by a separate server. Even though Django is really good for processing complex files and weaving data from disparate sources together, it is overkill and consumes more resources than it needs to, especially if the files never change. So Django supports allows you to specify static files using a special {% static ... %} tag in a HTML template.

Unfortunately, one of the hurdles that I faced was that many URLs to static files weren’t just embedded in the HTML templates, but were in fact found in the markdown that I dynamically convert into HTML and then insert into the template. So even if I updated the urls in the markdown to include the custom django static tag, these were part of an string of html that were dynamically inserted into the django template and wouldn’t get processed by django’s templating engine.

So in implementing static files for my website, I needed to consider how I was going to modify my markdown files to support the static environment.

How is this done? Typically you replace the URL that’s hard coded to a Django specific static tag like this.

First I had to create some very specific find and replace rules in order to find all the files within my markdown files that used local URLs. This was mostly a manual process, and I am okay with changing the actual markdown files to support the tag.

One of the other things not supported by Django templates is that you can’t dynamically process template tags inside other ones and have them process recursively, at least as far as I could go.

External Resources