django-libsass

0.9 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

To quickly integrate `django-libsass`, first ensure `django-compressor` is set up. Add `compressor` to `INSTALLED_APPS` and `compressor.finders.CompressorFinder` to `STATICFILES_FINDERS`. Then, configure `COMPRESS_PRECOMPILERS` in your `settings.py` to use `django_libsass.SassCompiler` for `text/x-scss` files. Finally, in your Django templates, load the `compress` and `static` tags and wrap your SCSS `link` tag within `{% compress css %}` and `{% endcompress %}` blocks, specifying `type="text/x-scss"`.

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;
    }
  }
}

view raw JSON →