Django.fun

Django loop refactor: javascript script to views.py in Highcharts

I have a script running that works, but I want to move it to "views.py". I can't seem to get the data I need because of the loop

Currently working script:

<script>
      Highcharts.chart('movements_container', {
              chart: {type: 'column'},
              title: {text: "{{account.name}} Income/Outcome"},
              plotOptions: {column: {stacking: 'normal', dataLabels: {enabled: true}}},
              tooltip: {pointFormat: "Current balance: {{account.balance}} DKK"},
              yAxis: {title: {text: "Movements"}},
              xAxis: {title: {text: "Income/Outcome"}, type: "datetime"},
              credits: {enabled: false},
              series: [
                      {name: "Balance",
                       data: [{{account.balance|floatformat}}],
                       stack: "balance"},
                      {% for movement in account.movements %}
                      {name: "{{account.name}}",
                       id: "account",
                       data: [{{movement.amount|floatformat}},],
                       stack: "expenses",
                       tooltip: {pointFormat: "Date: {{movement.timestamp}}</br>Transaction: {{movement.text}} </br> Amount: {{movement.amount}}"}},
                      {% endfor %}
                      ],
    });
</script>

As you can see, the big block above with "series" is what I want to translate to views.py:

 12 def account_details(request, pk):
 11     assert not request.user.is_staff, 'Staff user routing customer view.'
 10
  9     account = get_object_or_404(Account, user=request.user, pk=pk)
  8
  7     chart = {
  6             'chart': {'type': 'column'},
  5             'title': {'text': 'test'},
  4             'yAxis': {'title': {'text': "Movements"}},
  3             'xAxis': {'title': {'text': "Income/Outcome"}},
  2             'series': [
  1               # for movement in account.movements:
49                  {'name': account.name, 'data':str(account.movements)}
  1             ],
  3     }
  5
  6     context = {
  7         'account': account,
  8         'chart': json.dumps(chart),
 10     }
 11
 12     return render(request, 'bank/account_details.html', context)

My problem is to get the "account.movements" value under "series" in the highchart. What should be the approach?

Answers: 0