0

I am trying to pass my variable from Django's view to a javascript file. I read the stack question here (How can I pass my context variables to a javascript file in Django?) to get a few guidance but I am trying something a little bit different. I want to pass variable to the javascript file within that particular javascript reference script tag or changing the order of variable declaration. It is easier to show by example:

My somerandom.js:

console.log(variable);

What works:

{% extends base.html %}
{% load static %}
{% block body %}
    <h1> Hello, World! </h1>
{% endblock body %}
{% block javascript %}
    <script>
      var variable = {{ my_var }};
    </script>
    <script src="{% static "app1/bootstrap/js/somerandom.js" %}"></script>
{% endblock javascript %}

What doesn't work:

Edit: It doesn't work because the in developer tool (press F12 in Chrome) I see this error: Uncaught ReferenceError: variable is not defined

1:

{% extends base.html %}
{% load static %}
{% block body %}
    <h1> Hello, World! </h1>
{% endblock body %}
{% block javascript %}
    <script src="{% static "app1/bootstrap/js/somerandom.js" %}"></script>
    <script>
      var variable = {{ my_var }};
    </script>
{% endblock javascript %}

2:

{% extends base.html %}
{% load static %}
{% block body %}
    <h1> Hello, World! </h1>
{% endblock body %}
{% block javascript %}
    <script src="{% static "app1/bootstrap/js/somerandom.js" %}">
      var variable = {{ my_var }};
    </script>
{% endblock javascript %}

Reason I want those 2 to work is so I can organize and refer to my variables properly once my code gets longer. And in this link (http://www.mutaku.com/wp/index.php/2012/03/accessing-django-template-variables-in-javascript/), it shows that we can declare variable after referencing the javascript file.

8
  • what does not work? can you give us an error string, or some stack trace? What is the "error" you see? Commented Sep 8, 2017 at 2:56
  • I added the error message I see. Basically it says "variable is not defined". Commented Sep 8, 2017 at 3:00
  • 1
    If your somerandom.js tries to access variable it is normal to get this error as you declare it later. I highly doubt the method in the page you linked to would work. Commented Sep 8, 2017 at 6:44
  • somerandom.js is accessing variable (it basically outputs variable content through console). Is there any method whereby I can declare it anywhere? I want it to be called just by somerandom.js file and not by other file. Commented Sep 8, 2017 at 6:47
  • 1
    When you globally declare a JS variable as in this example it will be accessible by all functions no matter how you include it, ie. your method (2) is no different than (1). Commented Sep 8, 2017 at 6:54

2 Answers 2

2

When you use a <script> tag with a src= ... and a content in it the behavior is undefined, and is strongly related to the browser you're using: for instance with chrome the script is loaded from src=... and the content of the <script> is ignored
However the usage it's strongly discouraged

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

Comments

2

Enclosing the Django variable in double quotes worked for me. Then you invoke the .js file after the variable declaration.

<script>
    var variable = "{{ my_var }}";
</script>
<script type="text/javascript" src="{% static ... %}"></script>

This is because console.log({{ my_var }}) does not print the variable to the console but console.log("{{ my_var }}") does print it.

Comments

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.