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
- [Django]-Django: filtering by value in [None, other]
- [Django]-In Django, what does latest('date') return when there's more than one possible result?
- [Django]-Django modelfield, how do I get the actual value?
- [Django]-Django-Userena: adding extra non-null fields to a user's profile
- [Django]-Trying to understand Django select_for_update() with MySQL and Processes
Source:stackexchange.com