title: Django cumulative value displayed in template
body: <p>I’m new to Django and Python and have been plugging through Python Crash Course. I have finished that and now have been making my own Django project, adding some extra bits in.</p>
  <p>Currently, I have a table of entries from my model which I am displaying with a template, each entry in the table has a time and some other data. What I would like is the value for the cumulative time in a column, next to each entries time ie</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Lesson</th>
  <th>Lesson time</th>
  <th>cumulative time</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Lesson1</td>
  <td>0.5</td>
  <td>0.5</td>
  </tr>
  <tr>
  <td>Lesson2</td>
  <td>1</td>
  <td>1.5</td>
  </tr>
  <tr>
  <td>Lesson3</td>
  <td>1.3</td>
  <td>2.8</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I’m just really stuck on how to get this done, I have looked at lot’s of stack overflow and tried various ways that I just can’t get to work correctly. Looking at other examples they use annotate() and cumsum, but I’ve not been able to get that to work. I’ve done count and sum of other values but this has got me stuck. I think I need to loop over it in my view, but not sure how to associate each value to a cumulative time.</p>
  <p>Any help would be greatly received.
  Also, sorry this isn't very well written or succinct, programming doesn't come naturally to me.</p>
  <p>My <strong>topics.py</strong></p>
  <pre class="lang-py prettyprint-override"><code>def topics(request):
      &quot;&quot;&quot;Show all the topics&quot;&quot;&quot;
      
      # Get the public topics 
      public_topics = Lessontopic.objects.filter(public=True).order_by('date_added')
      # Get the private topics
      if request.user.is_authenticated:
          private_topics = Lessontopic.objects.filter(owner=request.user).order_by('date_added')
          topics =  private_topics
          &quot;&quot;&quot;By Adding private into topics, seeing all public topics not just users
          this way any topic added by authenticated user comes up in private, even if
          it is also visible in public&quot;&quot;&quot;
  
          time = Lessontopic.objects.aggregate(Sum('lesson_time'))
          total_time = time.get('lesson_time__sum')
          total_time = float(total_time)
          lessons = Lessontopic.objects.all().count()
          print(f&quot;There has been {lessons} lessons &quot;)
          solo = (Lessontopic.objects.filter(solo_lesson=True)
              .aggregate(Sum('lesson_time')))
          solo_time = solo.get('lesson_time__sum')
          print(f&quot;solo lesson flying time {solo_time} hours&quot;)
          solo_num = Lessontopic.objects.filter(solo_lesson=True).all().count()
          dual = (Lessontopic.objects.filter(solo_lesson=False)
              .aggregate(Sum('lesson_time')))
          dual_time = dual.get('lesson_time__sum')
          remaining_time = 50 - total_time    
          
          #Make a pie chart.
          labels = ['Solo', 'Dual']
          values = [solo_time, dual_time]
          chat = ['this is long']
          # Visualise the results
          data=[Pie(labels=labels,text=chat,values=values,pull=[0.1, 0],
              textposition='outside')
              ]
  
          x_axis_config = {'title': 'Hours'}
          y_axis_config = {'title': 'Lesson type'}
          my_layout = Layout(title='Flying time Solo vs Dual Instruction',title_font_color='black'    ,
                  title_font_size=30, title_x=0.5, legend_bordercolor='green', legend_borderwidth=5   )
          fig = ({'data': data, 'layout': my_layout})
          div = offline.plot(fig, auto_open=False, output_type='div')
  
          cumulative_time = Lessontopic.objects.annotate(
                              cumulative_times=Window(
                              expression = Sum('lesson_time'),
                              order_by=F('date_added').asc(),
                              frame=RowRange(start=None, end=0)
                              )
                          )
  
          context = {'topics': topics, 'cumulative_time':cumulative_time,'lessons':lessons, 'solo_time':solo_time,
                  'solo_num':solo_num, 'dual_time':dual_time,'solo_num':solo_num,
                  'remaining_time':remaining_time, 'dual':dual,'graph':div,}
  
  
      else:
          topics = public_topics
          context = {'topics': topics}
  
      return render(request, 'flying_logs/topics.html', context)
  </code></pre>
  <p>my <strong>model.py</strong></p>
  <pre class="lang-py prettyprint-override"><code>from django.db import models
  from django.contrib.auth.models import User
  from django.forms.widgets import DateInput 
  from django import forms
  import json
  import requests 
  
  # Create your models here.
  
  class Lessontopic(models.Model):
      &quot;&quot;&quot;The topic of each lesson or day flying&quot;&quot;&quot;
      text= models.CharField(max_length=200)
      date_added = models.DateTimeField(auto_now_add=True)
      owner = models.ForeignKey(User, on_delete=models.CASCADE)
      public = models.BooleanField(default=False, verbose_name=&quot;Make post public?&quot;,
          help_text=&quot;click on this form if you want it to be viewed by anyone&quot;)
      lesson_number =  models.PositiveIntegerField(blank=True, null=True)
      lesson_time = models.DecimalField('enter flight time', max_digits=2, decimal_places=1)
      solo_lesson = models.BooleanField(default=False, verbose_name=&quot;Were you  solo for the lesson&quot;,
              help_text=&quot;Click if were flying solo for the lesson&quot;)
      runway_direction = models.PositiveIntegerField(
               verbose_name=&quot;Select runway direction: 07 or 25&quot;)
      number_landings = models.PositiveIntegerField(blank=True, default=1,
                              help_text=&quot;Enter how many landings&quot;)
  
  
      date = models.DateTimeField()
  
      time_of_lesson = models.PositiveIntegerField(
               verbose_name=&quot;time that the lesson started&quot;)
  
      weather_data = models.CharField(max_length=200)
      adding = models.TextField(default=None, null=True, blank=True)
  
      def __str__(self):
          &quot;&quot;&quot;Return a string representation of the modeel&quot;&quot;&quot;
          return self.text
  
  </code></pre>
  <p>My template <strong>topics.html</strong></p>
  <pre class="lang-py prettyprint-override"><code>
  {% extends &quot;flying_logs/base.html&quot; %}
    &lt;h1&gt;Topics&lt;/h1&gt;
  {% block page_header %}
      {% endblock page_header %}  
  
  {% block content %}
  
  &lt;div class=&quot;container&quot;&gt;
  
      &lt;table class=&quot;table text-center  table-hover table-bordered&quot; &gt;
          
      &lt;tr&gt;&lt;tr class=&quot;table-primary&quot;&gt;
        &lt;th style=&quot;width: 10%&quot;&gt;Lesson No.&lt;/th&gt;
        &lt;th scope=&quot;col&quot;&gt;Date&lt;/th&gt;
        &lt;th scope=&quot;col&quot;&gt;Lesson&lt;/th&gt;
        &lt;th scope=&quot;col&quot;&gt;Dual/Solo
        &lt;th scope=&quot;col&quot;&gt;Flying time&lt;/th&gt;
        &lt;th scope=&quot;col&quot;&gt;Cumulative&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
  
          {% for topic in topics %}
  
              {% if topic.solo_lesson %} 
              &lt;tr class=&quot;table-success&quot;&gt;
                  {% endif %}         
                  &lt;td class=&quot;text-center&quot;&gt; {{topic.lesson_number}}&lt;/td&gt;
                  &lt;td&gt;{{ topic.date|date:'d/m/Y'}}&lt;/td&gt;
                  &lt;td&gt;&lt;a href=&quot;{% url 'flying_logs:topic' topic.id %}&quot;&gt;{{ topic }}&lt;/a&gt;&lt;/td&gt;
                  &lt;td&gt;{{ topic.solo_lesson|yesno:&quot;Solo, Dual&quot;  }}&lt;/td&gt;
                  &lt;td class=&quot;text-center&quot;&gt;{{topic.lesson_time}}&lt;/td&gt;
                  &lt;td&gt;Time so far{{ cumulative_time}}&lt;/td&gt;
              &lt;/tr&gt;
          {% endfor %}&lt;/table&gt;
          
  &lt;/div&gt;
  
  &lt;div class=&quot;container&quot;&gt;
      &lt;table class=&quot;table text-center table-hover table-bordered&quot; &gt;
      &lt;thread&gt;    
      &lt;tr&gt;&lt;tr class=&quot;table-primary&quot;&gt;
        
        &lt;th style=&quot;width: 5%&quot;&gt;Solo&lt;/th&gt;
        &lt;th style=&quot;width: 20%&quot;&gt;Dual&lt;/th&gt;
        &lt;th style=&quot;width: 20%&quot;&gt;total time&lt;/th&gt;
        &lt;th style=&quot;width: 20%&quot;&gt;Time remaing&lt;/th&gt;
        &lt;th style=&quot;width: 20%&quot;&gt; Solo Lessons&lt;/th&gt;
        &lt;th style=&quot;width: 35%&quot;&gt;Number of lessons&lt;/th&gt;
  
             
      &lt;/tr&gt;
    &lt;/thead&gt;
    
              
         
          
                  &lt;td&gt;{{solo_time|floatformat:1}}&lt;/td&gt;
                  &lt;td&gt;{{dual_time|floatformat:1}}&lt;/td&gt;
                  &lt;td&gt;{{total_time}}&lt;/td&gt;
  
                  {% if remaining_time &gt;= 0 %} 
                  &lt;td class=&quot;table-danger&quot;&gt;{{ remaining_time|floatformat:1 }}&lt;/td&gt;
                  {% elif remaining_time &lt;= 0 %}
                  &lt;td class=&quot;table-success&quot;&gt;You've done the minimum hours!!&lt;/td&gt;
          
  
                  {% endif %}
                  &lt;td class=&quot;text-center&quot;&gt; {{ solo_num}} &lt;/td&gt;
                  &lt;td&gt;{{lessons}}&lt;/td&gt;
  
        &lt;/tr&gt;
        &lt;/tr&gt;
        
        &lt;/table&gt;
        &lt;/div&gt;
  
  
  
      
      &lt;h3&gt;&lt;a href=&quot;{% url 'flying_logs:new_topic' %}&quot;&gt;&lt;button name=&quot;new entry&quot;
          class=&quot;btn btn-primary&quot;&gt;Add a new topic&lt;/button&gt;&lt;/a&gt;&lt;/h3&gt;
  
  {% if graph %}
  &lt;div style=&quot;width:600;height:500&quot;&gt;
  {{ graph|safe }}
  &lt;/div&gt;
  {% endif %}
  
  
  {% endblock content %}
  </code></pre>