[Django]-How to append django data to javascript

3👍

Create a dict in your view, dump it to JSON, and use it in the template.

data = data = [{'day': day, 'amt': amt} for day, amt in zip(sale_day, sale_amt)]
json_data = json.dumps(data)
return render(request, 'dashboard/index.html', {"data": json_data})

var data = {{ json_data|safe }}

0👍

Basically you want to serialize the Python dictionary into a JSON object. There are many ways to do this. The simplest is to use:

import json
json.dumps(data)

To serialize a Django model, take a look at: Django documentation: Serializing Django objects

👤Wtower

Leave a comment