Django {% static ‘path’ %} in javascript file

Since you are using django’s template language you can ONLY do this within your template between <script> tags. In other words if you wished to use your pic2.src javascript variable in an external script then you would need to declare it between <script> tags like so

    var pic2.src = "{% static"photos/1.jpg" %}"

And then you could access it in your external scripts that you might load like this:

<script type="text/javascript" src="{% static"js/my_external_script.js" %}"></script>

Regarding your question concerning load static and load staticfiles there is little distinction. Both act as a joiner for the STATIC_URL in your and the actual path to the file itself so both should work for your case. See here and here for more info.

Leave a Comment