First, order by date in your query:
events = Event.objects.order_by('date')
Then in your template, use the ifchanged tag
(https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#ifchanged):
{% for event in events %}
{% ifchanged event.date.month event.date.year %}
<h2>month here</h2>
{% endifchanged %}
<li>item here</li>
{% endfor %}
_Nik
On 6/20/2012 2:22 PM, grimmus wrote:
> Hi,
>
> I am trying to output upcoming events in the following format
>
> *June 2012*
> 12th - Event 1
> 15th - Event 2
>
> *July 2012*
> 1st - Event 3
>
> *August 2012*
> 20th - Event 4
>
> My model is very simple with just a title and dateTime field. I am
> unsure how i could output the months followed by the events in the
> style above. I guess nested loops would be necessary but would i need
> a separate loop for each month output ?
>
> Thanks for any tips.
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/RHAvjD4cKqUJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.