I have a small js script that I would like to put in a separate js file:

function user_click(sender, e){ var addressee = sender.id.substring(3); if(sender == e.target){ var base_url = "{% url 'user' 0 %}"; base_url = base_url.replace('0', addressee); document.location.href = base_url; } else{ var data = 'id=' + addressee; POST_AJAX(data, '{% url 'to_man' %}'); } } 

The trouble is that if I place it in a separate file in the static directory, as recommended by django, the magic of templates stops working: in particular, the url tag. Maybe, who knows any jango extensions that can render js-files from the static folder?

  • one
    Look at this answer at stackoverflow.com/a/37352754/5201699 the idea is to declare variables in the jango template, with the necessary values, which will then be used by your script. - Andrey
  • If your function is called on an event from the django template, then it is possible that you can pass arguments to the function when you call the function. for example onclick="user_click({% url 'user' 0 %})" - Andrey

0