Home > Software engineering >  How to use Django backend templet variable in JS
How to use Django backend templet variable in JS

Time:10-11

I want to use backend data in the js file.

return render(request, "class.html", {'all_classes':all_class})

I want to use it in the JS 'all_classes'. I know for the HTML -

<select name="subject_class" type="text"  required >
      {% for class in all_classes %}
          <option value="{{ class.class_name }}">{{ class.class_name }}</option>
      {% endfor %}
</select>

but please give me an idea for the javascript.

CodePudding user response:

try this: in your html page create script tag

<body>
.
.
.
.
.
<script>
    var all_class= {{ all_classes }};  
    console.log("all_classes",all_classes)
</script>
</body>

CodePudding user response:

you can construct the body of the script tag in the same way

<script>
    {% for item in some_variables %}
    let item.name=item.value; 
    {% endfor %}
<script>
  • Related