django-libsass
django-libsass is a Django Compressor filter that compiles Sass (SCSS) files using the `libsass-python` binding of LibSass. It integrates seamlessly into Django's static files and compressor pipeline, allowing developers to write Sass without external build tools. The current version is 0.9, and it receives infrequent but active maintenance updates for Django compatibility.
Warnings
- deprecated The underlying LibSass library is officially deprecated by the Sass team in favor of Dart Sass. While `libsass-python` and `django-libsass` will continue to receive maintenance updates, no new features or compatibility with future Sass language updates are planned. Consider this for long-term project planning.
- breaking Older versions of `django-libsass` may not be compatible with newer Django or `django-compressor` versions. For example, v0.6 fixed compatibility with `django-compressor` 1.6, and v0.9 added support for Django 3.1/3.2.
- gotcha When using CSS frameworks like Bootstrap that rely on high precision in Sass calculations, the default `LIBSASS_PRECISION` (5) might be insufficient, leading to layout issues. Bootstrap, for instance, typically requires a precision of 8.
- gotcha While `django-libsass` attempts to resolve `@import` paths using `STATICFILES_FINDERS`, explicitly traversing up the directory tree with `../` in `@import` statements might not work reliably and is discouraged by some related tools.
- gotcha Installing multiple Python packages that provide a `sass` module (e.g., `libsass` and other Sass processors) can lead to namespace conflicts and errors like `module 'object' has no attribute 'compile'`.
Install
-
pip install django-libsass django-compressor libsass
Imports
- SassCompiler
from django_libsass import SassCompiler
Quickstart
import os
# settings.py
INSTALLED_APPS = [
# ...
'compressor',
'django.contrib.staticfiles',
# 'your_app' (where your .scss files live in static/your_app/)
]
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
COMPRESS_PRECOMPILERS = (
('text/x-scss', 'django_libsass.SassCompiler'),
)
COMPRESS_ENABLED = True # Set to False in production for offline compression
COMPRESS_OFFLINE = False # Set to True in production for offline compression
# Optional: Configure additional libsass settings
# LIBSASS_OUTPUT_STYLE = 'compressed'
# LIBSASS_PRECISION = 8 # Recommended for frameworks like Bootstrap
# LIBSASS_ADDITIONAL_INCLUDE_PATHS = [
# os.path.join(BASE_DIR, 'path/to/extra/sass/includes'),
# ]
# templates/base.html
# ...
# {% load static %}
# {% load compress %}
# ...
# {% compress css %}
# <link rel="stylesheet" type="text/x-scss" href="{% static 'your_app/css/main.scss' %}">
# {% endcompress %}
# ...
# your_app/static/your_app/css/main.scss
// Example SCSS content
$primary-color: #337ab7;
body {
background-color: lighten($primary-color, 40%);
a {
color: $primary-color;
&:hover {
text-decoration: underline;
}
}
}