[Django]-Using template variables in Django with django-compressor

3👍

Your [.js|.coffee] files are not django templates, and will not be evaluated. Either you need to preprocess your scripts with django’s template renderer, or you set the variable in a html template, and assign it to a javascript window property. E.g.:

In your django template:

window.staticUrl = "{{ STATIC_URL }}";

{% load compress %}
{% compress js %}

//STATIC_URL in here does not load
<script type="text/coffeescript" charset="utf-8" src="/static/stuff.coffee" />  

{% endcompress %}

In your stuff.coffee:

$('#test').prepend "<img src="#{window.staticUrl}images/image.png" />"

Leave a comment