title: Double loop in Django template
body: <p>I have been trying to do this nested loop for couple of hours, but so far none of my results worked. Here is what i have so far:</p>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>{% for category in categories %}
      &lt;div class="col-md-12 column category list-group"&gt;
          &lt;p&gt;&lt;b&gt;{{ category.name }}&lt;/b&gt;&lt;/p&gt;
          {% for subcategory in subcategories %}
              &lt;div class="list-group-item"&gt;
                  &lt;h5 class="mb-1"&gt;&lt;a href="{% url 'subcategory_threads' subcategory.id %}"&gt;{{ subcategory.name }}&lt;/a&gt;&lt;/h5&gt;
                  &lt;small&gt;{{ subcategory.description }}&lt;/small&gt;
                  &lt;small&gt;{{ subcategory.count_threads }}&lt;/small&gt;
              &lt;/div&gt;
          {% endfor %}
      &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>class IndexView(ListView):
  template_name = 'myForum/index.html'
  context_object_name = 'SubCategory_list'
  queryset = SubCategory.objects.all()
  
  
  def get_context_data(self, **kwargs):
      context = super(IndexView, self).get_context_data(**kwargs)
      context['categories'] = Category.objects.all()
      context['subcategories'] = SubCategory.objects.all()
      return context
  </code></pre>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>class Category(models.Model):
  name = models.CharField(max_length=255)
  
  def __str__(self):
      return self.name
  
  
  class SubCategory(models.Model):
  name = models.CharField(max_length=255)
  description = models.CharField(max_length=255)
  category = models.ForeignKey('Category', default=0)
  
  def __str__(self):
      return self.name
  </code></pre>
  
  <p><strong>Output</strong>
  <a href="https://i.stack.imgur.com/Gg8Nj.png" rel="nofollow noreferrer">Output</a></p>
  
  <p>My problem is that SubCategory - News, belongs to Infromation, category Off-Topic belongs to General. For some reason loop displays all of subcategories, and i can't figure out how to narrow it to current Category.</p>