Skip navigation

Monthly Archives: April 2009

I just read Joshua Jonah’s post about Limiting HTTP requests with Django.  The gist is that he’s starting to get worried about all the different <link rel='stylesheet'>‘s and <script src='/some/js'>‘s in his document <head>, and we all know that each of those generates a separate HTTP request which will slow down your page’s load time.

My technique uses a view that builds a JS/CSS file with Django. The CSS/JS files that are compiled like this should be stored in your templates directory instead of your static media folder so they are accessible to the view…

…This allows you to used [sic] Django template language in your CSS files.

And I had an ah-HA! moment.  Could this be the way to use variables in your CSS files?  Having a view that defines a bunch of colors:

def buildcss(request):
linkColor = ‘#999’
linkHoverColor = ‘blue’

and passing that into the template that generates your CSS

a {
color: {{ linkColor }};

a:hover {
color: {{ linkHoverColor }};

Now, you probably don’t want have Django performing doing this kind of work for every request, so you might want to cache the URL that generates your CSS.  And I’m not sure if there are other downsides to this I haven’t considered, but it’s interesting to think about.