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?

2

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Got it. But the question I am asking is not that. It is how to include the static json1.json inside the script. Though, I will make the change in the question
are you using jquery?
No. I am currently using vanilla javascript.
is this JSON file is residing in the local server or external server?
it is present in the static folder
|

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.