0

I'm passing an array of files using this method

How to pass an array in Django to a template and use it with JavaScript

all is well if I have the script within the .html but if I want to have a separate example.js file, the only current way to get it working is to create the var in the html and then call it from example.js.

In an effort to keep things tidy and in one place, I'd like to avoid doing this. Is it possible to put it into the example.js ?

2
  • No, because the Django logic won't be interpreted within a JS file. The usual pattern to follow in these cases is to put the required Django output in the main HTML (either in data attributes or defined in JS variables contained in plain <script> blocks) and then access those from your external JS file. Commented Sep 20, 2018 at 9:27
  • Thanks Rory, this is the confirmation I required Commented Sep 20, 2018 at 14:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.