2022-06-05

Django objects with JavaScript

Assign javascript variable from django objects like so.

{% block javascript %}
<script>
  const todos = [
  {% for todo in object_list %}
  {
    id: "{{ todo.id }}",
    name: "{{ todo.name }}",
  },
  {% endfor %}
  ];
  
  todos.forEach((todo) => {
      // ...
  })
</script>
{% endblock javascript %}