CSS and JavaScript compression via django-compress
November 5, 2009 | frameworks, pythonauthor: Karol Zielinski | comments: 1 | views: 2524
Tags: compress, css, django, javascript, optimize, python, website
I want to automate CSS/JavaScript compression in my django application. My choose is: django-compress. Easy-to-use, fast and… it works.
Simple: I want to optimize and speed up my website. One of the most import thing to achieve this, is to compress CSS and JavaScript files. Compression is nothing more than just a removing formatting and white spaces, trimming class names, omitting unambiguous quotes around attributes, etc. This can reduce file size by (more or less) 60%!
Sure, yeah. That’s why I use django-compress.
Ok, let’s start fun…
First, download everything:
svn co http://django-compress.googlecode.com/svn/trunk/ django-compress
move this tool to your django application:
cd django-compress cp -r compress /opt/django-projects/my_app/
add it to your INSTALLED_APS:
cd /opt/django-projects/my_app/ vim settings.py
and add:
'my_app.compress'
to your INSTALLED_APS
Propably you don’t have csstidy installed. That’s why you should install it right now:
sudo apt-get install csstidy
vim settings.py
… and add all configuration options to this file. All kind of settings you have here.
My django-compress settings look like:
COMPRESS = True
COMPRESS_AUTO = True
COMPRESS_VERSION = True
COMPRESS_CSS = {
'compressed_css': {
'source_filenames': ('site.css', ),
'output_filename': 'all.r?.css',
'extra_context': {
'media': 'screen,projection',
},
},
}
COMPRESS_JS = {
'group_js': {
'source_filenames': ('js/jquery-1.3.2.js', 'js/functions.js', 'js/tweetmeme.js'),
'output_filename': 'js/all.r?.js',
},
'group_google_adv_left_js': {
'source_filenames': ('js/google_left_adv.js', 'external_js/show_ads.js'),
'output_filename': 'js/all_google_left_adv.r?.js',
},
'group_google_adv_right_js': {
'source_filenames': ('js/google_right_adv.js', 'external_js/show_ads.js'),
'output_filename': 'js/all_google_right_adv.r?.js',
},
'group_google_analytics_js': {
'source_filenames': ('external_js/ga.js', 'js/google_analytics.js'),
'output_filename': 'js/all_google_analytics.r?.js',
}
}
A little bit of translation…
If COMPRESS and COMPRESS_AUTO is enabled (True), the source files will be automatically updated, and re-generated IF NEEDED when invoked from the templatetags. The last modified time of the files will be compared, and if any of the source-files is newer than the output-file, the file will be re-generated.
I have COMPRESS and COMPRESS_AUTO enabled so it will be automatically updated and re-generated.
django-compress includes two template tags: compressed_css and compressed_js, in a template library called compressed.
They are used to output the <link> and <script>-tags for the specified CSS/JavaScript-groups (as specified in the settings). The first argument must be the name of the CSS/JavaScript group.
The templatetags will either output the source filenames or the compressed filenames, depending on the COMPRESS setting, if you do not specify the COMPRESS setting, the source files will be used in DEBUG-mode, and compressed files in non-DEBUG-mode.
So… I just need to add few things to my template file:
{% load compressed %}
at the beginning of file.
{% compressed_css 'compressed_css' %}
in <head /> area.
and all kind of JS groups (e.g. group_js, group_google_adv_left_js, etc) in <body /> area.
so we have for example…
{% compressed_js 'group_js' %}
in our <body />.
The end.
Restart your application, refresh your website in a web browser and preview your source code of the website.
There will be files like: js/all_google_right_adv.r123456.js, js/all_google_left_adv.r123456.js, all.r123456.css, js/all.r123456.js and js/all_google_analytics.r123456.js in it.
Hello, I'm Karol Zielinski, internet evangelist, an entrepreneur, project manager and a web developer from Gdynia, Poland. I like creative design, good advertisement, social media and all kind of stuff around the web.
November 5, 2009, 1:46 pm
Useful, thanks!
Have you ever tried django-compressor? It’s similar to django-compress. Here’s a link: http://github.com/mintchaos/django_compressor
I’ve never used either, actually, just wondering if anyone has experience with both, and an idea of their advantages/disadvantages.