
Django Compression and Storages
Django is an amazing Python-based framework for building web applications. It is highly useful for statistics and numerical computations to develop AI apps. However, it also faces a few issues that need to be resolved through integrations.
In our recent project, which is more or less similar to Airbnb, we face an issue related to CSS/JS browser caching.
The problem was, once we change something in the CSS/JS, that change was not getting reflected on the client side and browser was taking the old files from the cache.
To avoid this, we needed a mechanism to refresh the cache once anything has changed in the CSS/JS.
The obvious approach was to change the name or attach a version number to a CSS file each time we make a change. But we wanted this process to be automated so we came across Django-compressor.
Checkout 7 reasons Django Web Framework is Perfect for Startups
Django Compressor
Django-compressor creates a versioned CSS/JS file and stores it at a particular location provided by us. Django compress css creates a manifest.json file which has the mapping between the versioned files and the original files.
On the client side versioned CSS/JS files are rendered and not the original one in the Django pipeline.. It comes with an additional advantage that all these files are compressed and gzipped to reduce the loading time.
Each time there is a change in the file, we need to run the compress command which will generate a new versioned file so that the browser automatically replaces the file into its cache.
You can install django_compressor
by running the following command.
$ pip install django_compressor
We were serving our static files from Amazon S3. Django Compressor requires files to be compressed in the local file system cache. Django Compressor is basically a middle-ware.
Middle-ware acts as a hook between request and response. Django Compressor css provides hooks to automatically have compressed files pushed to a remote back-end storage.
Boto storage with Amazon S3
We used Amazon S3 to serve our static files so we need a backend storage to work with Amazon. We have used the boto backend storage from Django-storages.
Run the following command to install Django-storages
$ pip install django_storage
After this, we need to make the following changes in our settings.py to give the location of storage and for the compressed files.
- We have specified our Amazon S3 base URL in AWS_S3_CUSTOM_DOMAIN.
- Make sure the COMPRESS_ROOT and STATIC_ROOT settings need to be the same.
- We need to set COMPRESS_STORAGE and STATICFILES_STORAGE settings to our custom cached back-end storage.
- We need to set STATIC_URL and COMPRESS_URL to be the same in order to serve static files correctly.
- We also need to create a subclass to use S3 storage back-end to save files locally too.
As you can see we have used mysite.storage.s3utils.CachedS3BotoStorage as a value for STATICFILES_STORAGE, this basically gives us the path for the compressed files.
So we create a separate file named storage_backend.py inside project folder with following code
Changes in a Template
First, we need to load the compress tag at the top of the HTML file.
- Everything enclosed within
{% compress %}
block will be considered as a code to be compressed. - You need to create
{% compress css %}
block to compress CSS files. {% compress js % }
block is used to compress JS files.- We can use inline parameter inside
{% compress js inline %}
block for inline or local JS.
django_compression links the inline JS/CSS into a single cached file. So it will reduce the load from a browser and your response time become more faster.
Please note that you cannot use context variable inside inline js block if you are using compress.
Please refer the below example for invalid inline js.
Finally, we need to ask the framework to generate the compressed CSS/JS so we need to run collectstatic first and then the compress command as follows.
$ python manage.py collectstatic
$ python manage.py compress
And we are done, it will push all the CSS/JS to the S3 and changes will be reflected in the browser!
Consulting is free - let us help you grow!
