All I want to do is. . .
. . .use Coffeescript in a Django app.
Description: Using Coffeescript in Django is much simpler than you might think.
Contributors:
sloria
Updated: 07/14/13
Do these first:
-
Install Coffeescript
# Mac-specific instructions # Install node $ brew update $ brew install node
# In you shell profile (.bash_profile, .zshrc, etc.), add this line export PATH=/usr/local/share/npm/bin:$PATH
# Install coffee (notice the dash) npm install -g coffee-script
# To compile a file coffee -c cupofjoe.coffee # Watch mode--compile a file whenever it is changed coffee -cw cupofjoe.coffee
First, set up django-compressor.
# Install django-compressor
$ pip install django_compressor
Include the following in your settings.py
.
# settings.py INSTALLED_APPS = [ # Other apps... 'compressor', ] STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', # other finders.. 'compressor.finders.CompressorFinder', ) COMPRESS_PRECOMPILERS = ( ('text/coffeescript', 'coffee --compile --stdio'), )
That's it! You can now include .coffee
files in your html. Make sure to include them between {% compress js %}
tags and set the type
attribute for the script tag to text/coffeescript
.
# some_template.html {% compress js %} <script src="{{ STATIC_URL }}coffee/cupofjoe.coffee" type="text/coffeescript"></script> {% endcompress %}