title: How to regroup blog posts with multiple categories by category in Django template
body: <p>I have Blog with posts that have multiple categories</p>
  <pre><code>class BlogDetailPage(Page):
    heading = models.CharField(max_length=150, blank=False, null=False)
    categories = ParentalManyToManyField(&quot;blog.BlogCategory&quot;, blank=False)
    ...
  
  class BlogCategory(models.Model):
    title = models.CharField(max_length=30,unique=True)
    slug = AutoSlugField(populate_from='title')
    ...
  </code></pre>
  <p>My posts are as follows:</p>
  <ul>
  <li>Post 1: Category A</li>
  <li>Post 2: Category A, Category B</li>
  <li>Post 3: Category B</li>
  </ul>
  <p>I want regroup posts by category as below:</p>
  <p>Category A</p>
  <ul>
  <li>Post 1</li>
  <li>Post 2</li>
  </ul>
  <p>Category B</p>
  <ul>
  <li>Post 2</li>
  <li>Post 3</li>
  </ul>
  <p>My current solution is not giving me correct results.</p>
  <pre><code>{% regroup posts by categories.all as posts_by_categories %}
      &lt;ul&gt;
          {% for category in posts_by_categories %}
          &lt;li&gt;{{ category.grouper.0 }} # first category name
              &lt;ul&gt;
                  {% for post in category.list %}
                  &lt;li&gt;{{ post.id }}, {{post.categories.all}}&lt;/li&gt;
                  {% endfor %}
              &lt;/ul&gt;
          &lt;/li&gt;
          {% endfor %}
  </code></pre>
  <p>Amy ideas?</p>