0

I have the following in my template:

{% for showtime in showtimes %}
{% ifchanged showtime.start.day %}
<h3>{{ showtime.start|date:"M d" }}</h3>
{% endifchanged %}

showtime.start is a timezone aware DateTimeField. The problem is that showtime.start.day returns the day in UTC while showtime.start|date:"M d" prints the date in the context timezone.

I've tried the following:

{% ifchanged showtime.start|date:"M d" %}

which fails since ifchanged can't deal with arguments to filters (even in 1.5). I've tried playing around with timezone and localtime, eg.

{% load tz %}
{% timezone TIME_ZONE %}
{% for showtime in showtimes %}
{% ifchanged showtime.start.day %}
<h3>{{ showtime.start|date:"M d" }}</h3>
{% endifchanged %}
{% endtimezone %}

As expected those don't work. Is there any way to make ifchanged work on the timezoned version of the date time or do I need a custom filter?

Rob Osborne
  • 4,897
  • 4
  • 32
  • 43
  • Hey i have exactly the same problem i was wondering what you end up doing? My question http://stackoverflow.com/questions/21199353/ouput-timezone-aware-django-datetime-fields-without-filters – Guillermo Siliceo Trueba Jan 18 '14 at 04:12

1 Answers1

0

Unfortunately the only method I found is with a custom template tag:

{% ifchanged showtime.start|localtime|year_month_day %}

So I filter the date using localtime and then the custom template tag returns the year, month and day in the proper timezone so ifchanged works.

@register.filter()
def year_month_day(value):
    return "%d-%d-%d" % (value.year, value.month, value.day)
Rob Osborne
  • 4,897
  • 4
  • 32
  • 43