Skip navigation

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):
…snip…
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.

About these ads

4 Comments

  1. I had the exact same idea! I read in the docs that serving static files via django wasn’t recommended for production environments, and that it could have serious security holes, but I think this would be great for trying out different color palettes in your css.

    Imagine being able to pick a new palette from colourlovers.com and see how it would look on your site after three seconds!

    Possible downsides are that the user’s browser won’t be able to cache the stylesheet and an increased load on the server, but this could be a test server only kind of thing.

    Let me know if you try it out!

  2. Follow up:

    Just found a cgi utiliy that accomplishes this: http://moonfall.org/

    Apparently people have been doing this in PHP for a while: http://www.barelyfitz.com/projects/csscolor/

    The moonfall option seems rather simple to me, but I do think I would prefer django templates.

  3. It’s always weird opening links on Reddit and seeing your own name.

    I liked the template language includes for the ability to use “{{ MEDIA_URL }}” and reverses in my url() references. This is a major problem with the default Django setup. It’s basically the only non DRY part left.

    @Nick: In my scheme, the url makes reference to the files included in the css. As long as the css does not change dynamically according to some value, you can cache it fine. I am doing this on a production server and it works great under load.

    Nice article and thanx for the link! :D

  4. I highly enjoyed reading this post, keep on posting such interesting stuff!!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: