Home

Awesome

django-sass

The absolute simplest way to use Sass with Django. Pure Python, minimal dependencies, and no special configuration required.

Source code on GitHub

Status

Python PackagePyPI - Python Version PyPI - Django Version PyPI - Wheel PyPI - Downloads PyPI
BuildBuild Status Azure DevOps tests (branch) Azure DevOps coverage (branch)

Installation

  1. Install from pip.
pip install django-sass
  1. Add to your INSTALLED_APPS (you only need to do this in a dev environment, you would not want this in your production settings file, although it adds zero overhead):
INSTALLED_APPS = [
    ...,
    'django_sass',
]
  1. Congratulations, you're done 😀

Usage

In your app's static files, use Sass as normal. The only difference is that you can not traverse upwards using ../ in @import statements. For example:

app1/
|- static/
   |- app1/
      |- scss/
         |- _colors.scss
         |- app1.scss
app2/
|- static/
   |- app2/
      |- scss/
         |- _colors.scss
         |- app2.scss

In app2.scss you could reference app1's and app2's _colors.scss import as so:

@import 'app1/scss/colors';
@import 'app2/scss/colors';
// Or since you are in app2, you can reference its colors with a relative path.
@import 'colors';

Then to compile app2.scss and put it in the css directory, run the following management command (the -g will build a source map, which is helpful for debugging CSS):

python manage.py sass app2/static/app2/scss/app2.scss app2/static/app2/css/app2.css -g

Or, you can compile the entire scss directory into a corresponding css directory. This will traverse all subdirectories as well:

python manage.py sass app2/static/app2/scss/ app2/static/app2/css/

In your Django HTML template, reference the CSS file as normal:

{% load static %}
<link href="{% static 'app2/css/app2.css' %}" rel="stylesheet">

✨✨ Congratulations, you are now a Django + Sass developer! ✨✨

Now you can commit those CSS files to version control, or run collectstatic and deploy them as normal.

For an example project layout, see testproject/ in this repository.

Watch Mode

To have django-sass watch files and recompile them as they change (useful in development), add the --watch flag.

python manage.py sass app2/static/app2/scss/ app2/static/app2/css/ --watch

Example: deploying compressed CSS to production

To compile minified CSS, use the -t flag to specify compression level (one of: "expanded", "nested", "compact", "compressed"). The default is "expanded" which is human-readable.

python manage.py sass app2/static/app2/scss/ app2/static/app2/css/ -t compressed

You may now optionally commit the CSS files to version control if so desired, or omit them, whatever fits your needs better. Then run collectsatic as normal.

python manage.py collectstatic

And now proceed with deploying your files as normal.

Limitations

Feel free to file an issue or make a pull request to improve any of these limitations. 🐱‍💻

Why django-sass?

Other packages such as django-libsass and django-sass-processor, while nice packages, require django-compressor which itself depends on several other packages that require compilation to install.

Installing django-compressor in your production web server requires a LOT of extra bloat including a C compiler. It then will compile the Sass on-the-fly while rendering the HTML templates. This is a wasteful use of CPU on your web server.

Instead, django-sass lets you compile the Sass locally on your machine before deploying, to reduce dependencies and CPU time on your production web server. This helps keep things fast and simple.

django-sass only depends on libsass (which provides pre-built wheels for Windows, Mac, and Linux), and of course Django (any version).

Programmatically Compiling Sass

You can also use django-sass in Python to programmatically compile the sass. This is useful for build scripts and static site generators.

from django_sass import compile_sass

# Compile scss and write to output file.
compile_sass(
    inpath="/path/to/file.scss",
    outpath="/path/to/output.css",
    output_style="compressed",
    precision=8,
    source_map=True
)

For more advanced usage, you can specify additional sass search paths outside of your Django project by using the include_paths argument.

from django_sass import compile_sass, find_static_paths

# Get Django's static paths.
dirs = find_static_paths()

# Add external paths.
dirs.append("/external/path/")

# Compile scss and write to output file.
compile_sass(
    inpath="/path/to/file.scss",
    outpath="/path/to/output.css",
    output_style="compressed",
    precision=8,
    source_map=True,
    include_paths=dirs,
)

Contributing

To set up a development environment, first check out this repository, create a venv, then:

(myvenv)$ pip install -r requirements-dev.txt

Before committing, run static analysis tools:

(myvenv)$ black .
(myvenv)$ flake8
(myvenv)$ mypy

Then run the unit tests:

(myvenv)$ pytest

Changelog

1.1.0

1.0.1

1.0.0

0.2.0

0.1.2

0.1.1

0.1.0