skip to Main Content

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" class="form-control" 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.

2

Answers


  1. try this:
    in your html page create script tag

    <body>
    .
    .
    .
    .
    .
    <script>
        var all_class= {{ all_classes }};  
        console.log("all_classes",all_classes)
    </script>
    </body>
    
    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search