Django Assetfiles is a drop-in replacement for staticfiles which handles asset processing.
-
Install package from PyPi:
$ pip install django-assetfiles
-
Replace
'django.contrib.staticfiles'
inINSTALLED_APPS
with'assetfiles'
:INSTALLED_APPS = ( # ... # 'django.contrib.staticfiles', 'assetfiles', )
-
That's it! Assetfiles will default to your Staticfiles settings.
-
Add an asset file that should be processed:
// static/css/main.scss $color: red; body { color: $color; }
-
Add a link to the processed CSS file in your template (you can use standard Staticfiles conventions):
{% load staticfiles %} <link href="{% static 'css/main.css' %}" rel="stylesheet">
Assetfiles will automatically serve up the processed version of
main.scss
at the static url of/static/css/main.css
. -
To serve assets in development, either use
runserver
as normal or add the following to yoururls.py
:from assetfiles.urls import assetfiles_urlpatterns # ... the rest of your URLconf goes here ... urlpatterns += assetfiles_urlpatterns()
-
For deployment, run
collectstatic
as usual and Assetfiles will process and copy over the assets:$ python manage.py collectstatic $ cat public/css/main.css body { color: red; }
Copyright (c) 2012 LocalMed, Inc.. See LICENSE for details.