3

To call a Django view from HTML we use the following:

"{% url 'namespace: view_fun' %}"

But how to do this from Javascript?

Thank you!

Nenad Savic
  • 135
  • 2
  • 9

1 Answers1

4

One hacky way to do it would be to include following script in your template that references the javascript file.

<script>
    var fun_url = "{% url 'namespace: view_fun' %}";
</script>

And then expecting this fun_url variable in your referenced javascript file.

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
  • Does not solve what I want. I need this in JS script which is a separate file. It should fit somehow in the following: window.location.href = ... – Nenad Savic Mar 23 '17 at 10:00
  • It does solve it. You can't treat js files as templates, but you *can* share js variables in the same DOM. – shad0w_wa1k3r Mar 23 '17 at 10:07