Django Compressor Tutorial
Django Compressor Tutorial
in
Nitin Raturi's Software Follow
Engineering Blog
In this tutorial, you will learn how to compress and optimize your static files i.e.,
javascript (.js) and CSS (.css) files for production. You will work with django-
compressor to compress CSS and JS and django-htmlmin to minify HTML files. You
can refer to the source code of this project here.
Django Compressor is a python package for the Django web framework to
compress and minify CSS and JS files using tag. {% load compress %}
Install Dependencies
To optimize static files, you need to install the following dependencies. First, let's
install memcached for caching static files. It's optional but it will speed up your static
files serving, so totally worth it. Try it
COPY
# Install Memcache
sudo apt-get install Memcached # Ubuntu
brew install memcached # Mac
# Start memcache
sudo service memcached start # Ubuntu
brew services start memcached # Mac
installing pip packages. You can learn more about virtual environment here.
COPY
Configure Memcached
COPY
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'compressor',
]
Add middlewares
COPY
MIDDLEWARE = [
'django.middleware.gzip.GZipMiddleware', #This one
'htmlmin.middleware.HtmlMinifyMiddleware', #This one
'htmlmin.middleware.MarkRequestMiddleware', #This one
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR,"static_root")
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# Add this
'compressor.finders.CompressorFinder',
)
COMPRESS_ENABLED = True
COMPRESS_CSS_HASHING_METHOD = 'content'
COMPRESS_FILTERS = {
'css':[
'compressor.filters.css_default.CssAbsoluteFilter',
'compressor.filters.cssmin.rCSSMinFilter',
],
'js':[
'compressor.filters.jsmin.JSMinFilter',
]
}
HTML_MINIFY = True
KEEP_COMMENTS_ON_MINIFYING = True
{% load static %}
{% load compress %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
{% compress css %}
<link rel="stylesheet" href="{% static 'style.css' %}">
{% endcompress %}
{% compress js %}
<script src="{% static 'script.js' %}"></script>
{% endcompress %}
</head>
<body>
<center>
<h1>Raturi.in</h1>
<h1 class="heading">Hello Lets compress</h1>
<p class="para">I am just a para</p>
<button onclick="change_colors()">Change colors</button>
</center>
</body>
</html>
# CSS
<link rel="stylesheet" href="/static/CACHE/css/output.f7c661b7a124.css" type
="text/css" charset="utf-8">
# JS
<script src="/static/CACHE/css/output.f7c661b7a124.css" type="text/css" chars
et="utf-8"></script>
# Run your server and you can check your source code to verify it.
Now if you check your source code, you will see that your files are compressed.
For production, in your settings.py set COMPRESS_ENABLED like below
COPY
{% load compress %}
{% compress <js/css> [<file/inline/preload> [block_name]] %}
<html of inline or linked JS/CSS>
{% endcompress %}
{% compress css %}
<link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css" ch
arset="utf-8">
{% compress js %}
<script defer type="text/javascript" src="{% static 'custom/js/lozad.min.js'
%}"></script>
<script>
function loadAdsenseScript(url) {
let script = document.createElement('script');
script.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygo
ogle.js";
script.defer = true;
script['data-ad-client'] = "ca-XXX";
document.head.appendChild(script);
}
</script>
{% endcompress %}
Output
COPY
You can add inline parameter if you want content directly rendered to the page
instead of a separate file
COPY
{% compress js inline %}
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('config', 'G-asdww');
</script>
{% endcompress %}
You can also add preload parameter to generate the preload tag for the compressed
file in the template
COPY
{% compress js preload %}
<script>
// On page load or when changing themes, best to add inline in `head` to
avoid FOUC
if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in
localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches))
{
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark')
}
</script>
{% endcompress %}
Output:
COPY
That's all for a setup of the Django compressor. You can check the source code of
this project here.
You can also check out more django-tutorials.
Subscribe to my newsletter
Read articles from Nitin Raturi's Software
Engineering Blog directly inside your inbox.
Subscribe to the newsletter, and don't miss out.
Enter your email address SUBSCRIBE
Django
Written by
Nitin Raturi Follow
Passionate about software engineering.
MORE ARTICLES
Nitin Raturi Nitin Raturi
Nitin Raturi
Dockerized Django
(Production ready in 1 click)
Hi reader, Presenting to you a turnkey
solution that allows you to effortlessly
launch your Django s…
Tue Feb 13 2024 11:02:39 GMT+0530 (India Standard Time)