title: Iterate through an object in Django template
body: <p>In Django 1.9, I have a database table that contains car brands.  I am trying to build an index (like one found at the back of a textbook) of car brands.  For example:</p>
  
  <p><strong>A</strong><br>
  Aston Martin<br>
  Audi<br>
  ...<br>
  <strong>B</strong><br>
  Bentley<br>
  BMW<br>
  ...</p>
  
  <p>Here is code from my <strong>view.py</strong>:</p>
  
  <pre><code>def home(request):
  
      car_index = {}
      alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N',
          'O','P','Q','R','S','T','U','V','W','X','Y','Z']
  
      for letter in alphabet:
          car_index[letter] = {}
          car_index[letter]['title'] = letter
          car_index[letter]['brands'] = Cars.objects.filter(brand__startswith = letter)
  
      return render(request, 'cars/home.html', {'car_index':car_index})
  </code></pre>
  
  <p>Here is code from my <strong>home.html</strong> template:  </p>
  
  <pre><code>{% for each in car_index %}
  
      {{ each.title }}
  
      {% for brand in each.brands %}
          &lt;a href="{{ brand.link }}"&gt;{{ brand.name }}&lt;/a&gt;&lt;br&gt;
      {% endfor %}
  
  {% endfor %}
  </code></pre>
  
  <p>In view.py, I have tried <code>.values()</code> in the queryset, <code>.items()</code> in the template context.  In the template, I have tried <code>car_index.items</code>, <code>each.brands.items</code>, <code>each.brands[0]</code>.  Nothing works.  With the code above as is, I get the titles: E D X I A U H B T S N K Q Z J Y W V O L R F G P C M, but no links.  (I know how to sort, but working on links first)</p>
  
  <p>I have read:</p>
  
  <p><a href="https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#for" rel="nofollow noreferrer">https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#for</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8018973/how-to-iterate-through-dictionary-in-a-dictionary-in-django-template?rq=1">how to iterate through dictionary in a dictionary in django template?</a></p>