Using LESS in Django
To setup django to precompile LESS files before serving to the client:
-
Install django-compressor
-
Define
STATIC_ROOT
(orCOMPRESS_ROOT
) but usuallySTATIC_ROOT
will probably be used by others already so it’s ok. I just got to undestand thatSTATIC_ROOT
is actually the folder thatpython manage.py collectstatic
will transfer all the static files to to be deployed. -
In your base.html or where you load the LESS file, use /* base.html */
{% compress css %}
<link rel="stylesheet" href="{% static 'styles/app.less' %}".
type="text/less" media="all">
{% endcompress %}
And that’s done for setting up compiling LESS file. For using LESS files, I defined a main.less
file that imports all css/less files in the project and include that file in base.html file. Here’s an example:
/* main.less */
@import "/static/bower_components/bootstrap/dist/css/bootstrap.min.css";
@import "/static/bower_components/font-awesome/css/font-awesome.min.css";
...
/* some other files */
...