Django template system: how do I solve this loop / grouping / counting?

I have a list of articles, and each article belongs to a section.

class Section(models.Model):
  name = models.CharField(max_length=200)

  def __unicode__(self):
    return self.name

class Article(models.Model):
  section = models.ForeignKey(Section)
  headline = models.CharField(max_length=200)
  # ...

I want to display articles grouped by topic.

Sponsorships, Advertising & Marketing
1. Nike To Outfit All 18 Univ. Of Memphis Athletic Teams
2. Phil Jackson Questions Harrah Signage At New Orleans Arena
3. Puma Hires NY-Based Ad Agency Droga5 To Lead Global Account
4. Pizza Patrón To Replace Pizza Hut As AAC Exclusive Provider
5. Marketplace Roundup

Sports media
6. Many Patriots Fans In New England Will Not See Tonight Game
7. ESPN Ombudsman Says Net Should Have Clarified Holtz Situation
8. EA Sports To Debut Fitness Title For Nintendo Wii In Spring '09
9. Blog Hound: Rockets-Suns Scuffle Today No.1 Topic
10. Media Notes

Leagues & Governing Bodies
11. DEI, Chip Ganassi Racing To Merge Into Four-Car Sprint Cup Team
12. NASCAR Roundtable Part II: New Strategies, Cutbacks Discussed
13. Average Ticket Price For NFL Playoff Games To Drop By 10%

I figured out how to do most of this using the Django template system.

{% regroup articles by section as articles_by_section %}

{% for article in articles_by_section %}    
    <h4>{{ article.grouper }}</h4>
    <ul>
    {% for item in article.list %}  
        <li>{{ forloop.counter }}. {{ item.headline }}</li>
    {% endfor %}
    </ul>
{% endfor %}

I just can't figure out how to make numbers. In the above code, the article numbers in Sports Media are 1-5 instead of 6-10. Any suggestions?

+3
source share
4 answers

Following Jeb suggeston in the comment, I created a custom template tag .

I replaced {{ forloop.counter }}with {% counter %}, a tag that simply prints how many times it has been called.

Here is the code for my counter.

class CounterNode(template.Node):

  def __init__(self):
    self.count = 0

  def render(self, context):
    self.count += 1
    return self.count

@register.tag
def counter(parser, token):
  return CounterNode()
+4
source

This is not completely neat, but may be suitable for someone:

{% for article in articles %}        
   {% ifchanged article.section %}
      {% if not forloop.first %}</ul>{% endif %}
      <h4>{{article.section}}</h4>
      <ul>
   {% endifchanged %}
          <li>{{forloop.counter}}. {{ article.headline }}</li>
   {% if forloop.last %}</ul>{% endif %}
{% endfor %}
+1

, forloop.parentloop.counter , .

-1

:

{% regroup articles by section as articles_by_section %}

<ol>
{% for article in articles_by_section %}        
    <h4>{{ article.grouper }}</h4>
    {% for item in article.list %}  
        <li>{{ item.headline }}</li>
    {% endfor %}
{% endfor %}
</ol>
-1

Source: https://habr.com/ru/post/1699567/


All Articles