1

How can I load some variable from another js document inside a template, and then modify this variable inside a script tag in my template?

For example:

js file

let myObject = {color: red}

Django template

{% extends 'app/general/base.html' %}
{% load crispy_forms_tags %}
{% load widget_tweaks %}
{% load static %}

{% block content %}
    <h1 id="color"></h1>

    <script>
        document.getElementById('color').innerHTML = myObject.color //I need some way of loading myObject here, and be able to retrieve and edit data from it
    </script>
{% endblock %}

1 Answer 1

1

That is simple.

<script src="path_to_your_file.js"></script>
<script>        
    console.log(myObject);
</script>

Where path_to_your_file.js is the path to the file where the myObject is present

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

5 Comments

I just console logged the object here just to show how to do that. You can now do whatever you want with that object
That is not working. link
chartObject is an object inside the file 'chart-area-demo.js'
The problem was that my cache was not clear. I had to do a ctrl + F5 to be able to apply the changes.
This is the way how we plugin the external js into our templates.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.