[Answered ]-How can I include template tags in static files in Django?

2👍

Short answer is no. But you can circumvent this, there’s two approaches I can think of:

  1. add a script with the urls you need in your base html template, they need to be available globally so that you can access them with other scripts
  2. make a script to generate a .js file with all your urls and place it with all the other staticfiles (django translations for js use a similar approach)

Approach 1, would be something like:

<html>
...
<script>
  var myApp = {
    URLS: {
      login: {% url 'login' %},
      welcome: {% url 'welcome' %},
      ...
    }
  }
</script>
<script>console.log("The login url is " + myApp.URLS.login + "!")</script>
<script src="script/that/uses/urls.js"></script>
...
</html>

Leave a comment