Displaying events by month

27 views
Skip to first unread message

grimmus

unread,
Jun 20, 2012, 5:22:23 PM6/20/12
to django...@googlegroups.com
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.

Nikolas Stevenson-Molnar

unread,
Jun 20, 2012, 6:21:28 PM6/20/12
to django...@googlegroups.com
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
--
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 django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


grimmus

unread,
Jun 24, 2012, 4:24:05 AM6/24/12
to django...@googlegroups.com
Thank you Nikolas. Works perfect :)

To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages