Alexis
Alexis

Reputation: 25193

Django template variables in linked javascript

I have javascript in the form <script src="/static/file.js" /> in a Django template. I would like this referenced file.js to get the {{ STATIC_URL }} and other template variables.

What would be the best way to give access to file.js to the template variables?

Upvotes: 1

Views: 151

Answers (1)

Mariusz Jamro
Mariusz Jamro

Reputation: 31663

Render the desired variables in your main layout file, before you include file.js:

<script>
    var STATIC_URL = '{{ STATIC_URL }}';
</script>
<script src="/static/file.js" />
#include other scripts here, STATIC_URL will be available as regular variable

Upvotes: 1

Related Questions