C
C
connor742015-07-16 14:21:16
Django
connor74, 2015-07-16 14:21:16

How to display data grouped by dates on the page?

Good time of the day!
There is a model:

class Data(models.Model):

  date_ready = models.DateField('На дату')
  body = models.TextField('Задание по предмету', default='Не установлено')
  subject = models.ForeignKey('Subjects', verbose_name='Предмет')

You need to display data on the page as follows:
Date: dmY (latest)
------------------------------------ -----------------
- subject body 1
- subject body 2
...
Date: dmY (penultimate)
---------------- ------------------------------
...
etc.
That is, first the output of the date, and then the rest of the fields related to this date without the date itself. That is grouping around the date. Where (in the model, view or template) and how best to do it?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Lyutoev, 2015-07-16
@connor74

Look towards the regroup template tag - djbook.ru/rel1.8/ref/templates/builtins.html#regroup
In views.py
In templates

{% regroup data by date_ready as data_by_date %}

<ul>
{% for data in data_by_date %}
    <li>{{ data.grouper }}
    <ul>
        {% for item in data.list %}
          <li>{{ item.subject  }} {{ item.body }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question