title: Find Sum of Columns of Data in Django Like and Excel File
body: <p>I am using Django 1.8.3 and Python 3.4.3</p>
  
  <p>Comparing my issue to an Excel file is the best way I can explain my challenge. I have a ListView that is displaying a table of data in my template.  The left column are the months January through December, with value fields across the top (just like an Excel Spreadsheet)  I need to add the value of the "columns" to render a "total" Sum.  I've read several solutions, but I can't seem to figure it out is a Pythonic way.</p>
  
  <p>Below are snippets of my code that will hopefully give you a good idea of what I need to do.</p>
  
  <p><strong>Solution Needed:</strong> I need to get the total value of each months Recipients table, which will be displayed in a Master matrix (not shown here)</p>
  
  <p>Thank you for your help.  If you do help, please provide template tag example as well.</p>
  
  <p>index.html (Only showing January &amp; February, but the list goes to December)</p>
  
  <pre><code>&lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;Quantity&lt;/th&gt;
      &lt;th&gt;Recipients&lt;/th&gt;
      &lt;th&gt;Unsubscribes&lt;/th&gt;
      &lt;th&gt;Bounces&lt;/th&gt;
      &lt;th&gt;Open&lt;/th&gt;
      &lt;th&gt;Clicks&lt;/th&gt;
      &lt;th&gt;Open Rate&lt;/th&gt;
      &lt;th&gt;CTR&lt;/th&gt;
      &lt;th&gt;Sales&lt;/th&gt;
      &lt;th&gt;IFS SignUps&lt;/th&gt;
      &lt;th&gt;Traffic&lt;/th&gt;
      &lt;th&gt;T/S Ratio&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      {% if email_list %}
        &lt;td&gt;January&lt;/td&gt;
        &lt;td&gt;{{ total_campaigns.January | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_recipients.January | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_unsubscribes.January | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_bounces.January | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_open.January | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_clicks.January | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{% average total_open.January total_recipients.January %}&lt;/td&gt;
        &lt;td&gt;{% average total_clicks.January total_open.January %}&lt;/td&gt;
      {% endif %}
      {% if traffic_list %}
        &lt;td&gt;{{total_sales.January | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{total_ifs_signups.January | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{total_traffic.January | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{% ratio total_traffic.January total_sales.January %}&lt;/td&gt;
      {% endif %}
    &lt;/tr&gt;
    &lt;tr&gt;
      {% if email_list %}
        &lt;td&gt;February&lt;/td&gt;
        &lt;td&gt;{{ total_campaigns.February | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_recipients.February | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_unsubscribes.February | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_bounces.February | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_open.February | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{ total_clicks.February | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{% average total_open.February total_recipients.February %}&lt;/td&gt;
        &lt;td&gt;{% average total_clicks.February total_open.February %}&lt;/td&gt;
      {% endif %}
      {% if traffic_list %}
        &lt;td&gt;{{total_sales.February | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{total_ifs_signups.February | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{{total_traffic.February | default_if_none:"0" }}&lt;/td&gt;
        &lt;td&gt;{% ratio total_traffic.February total_sales.February %}&lt;/td&gt;
      {% endif %}
    &lt;/tr&gt;
  
    ... ( This repeats through the month of December )
  </code></pre>
  
  <p>views.py ( sample data from my views file )</p>
  
  <pre><code>    class DashboardView(TemplateView):
          template_name = "dashboard/pages/index.html"
  
          def get_context_data(self, **kwargs):
              context = super(DashboardView, self).get_context_data(**kwargs)
              context['email_list'] = Email.objects.all()
              context['traffic_list'] = Traffic.objects.all()
  
              days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
              months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
                        'November', 'December']
  
              total_campaigns = {}
              campaign_totals = {}
              total_recipients = {}
              total_unsubscribes = {}
              total_bounces = {}
              total_open = {}
              total_clicks = {}
              total_sales = {}
              total_ifs_signups = {}
              total_traffic = {}
  
              for month in months:
                  # total count
                  total_campaigns[month] = Email.objects.filter(month=month).count()
                  # recipients
                  total_recipients[month] = Email.objects.filter(month=month).aggregate(
                      Sum('recipients')).get('recipients__sum', 0.00)
                  # unsubscribes
                  total_unsubscribes[month] = Email.objects.filter(month=month).aggregate(
                      Sum('unsubscribes')).get('unsubscribes__sum', 0.00)
                  # bounces
                  total_bounces[month] = Email.objects.filter(month=month).aggregate(Sum('bounces')).get(
                      'bounces__sum', 0.00)
                  # opens
                  total_open[month] = Email.objects.filter(month=month).aggregate(
                      Sum('open')).get('open__sum', 0.00)
                  # clicks
                  total_clicks[month] = Email.objects.filter(month=month).aggregate(
                      Sum('clicks')).get('clicks__sum', 0.00)
  
  ...
  </code></pre>