1

I want to load the staticfile json that is present in the code into the js to host the swagger.

My index.html:

<HTML>
<head>
{% load static %}
</head>
<body>
<script src="{% static "js/script1" %}"></script>
<script>
var json_contents = // Use the static json `js/json1.json` in here?
</script>
</body>
</html>

How to do the above?

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
phoenix97
  • 179
  • 3
  • 11

1 Answers1

1

You have misspelled the static template tag

{% load static %} <!-- not "staticfiles" -->
<!DOCTYPE html>
<HTML>
  <body>
  <script src="{% static "js/script1.js" %}"></script> <!-- missed .js extention -->
  <script>

    async function load() {
        let url = '{% static "js/json1.json" %}';
        let json_contents = await (await fetch(url)).json();
        console.log(json_contents);
    }
    load();

  </script>
  </body>
</html>

Documentation: Configuring static files

Sumithran
  • 6,217
  • 4
  • 40
  • 54