All I want to do is. . .

. . .use Coffeescript in a Django app.

OS:
Tags:

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
    
     [full instructions]

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

Raw: use-coffeescript-in-django.md


If you have suggestions, corrections, or content to contribute, fork us at our Github repo or open an issue.

Licensed under the CC-SA.