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.