compare dates in the template language

1,007 views
Skip to first unread message

tjdon...@gmail.com

unread,
Aug 3, 2006, 12:21:31 AM8/3/06
to Django users
Hi,

Is there any way to compare two dates in the Django template language?

For example, I'd like to if the date variable due_date is before or
after the current date. The timesince and timeuntil filters work, but
they seem to require that I know ahead of time the order of the dates
I'm comparing.

Toby

Malcolm Tredinnick

unread,
Aug 3, 2006, 12:59:45 AM8/3/06
to django...@googlegroups.com
On Thu, 2006-08-03 at 04:21 +0000, tjdon...@gmail.com wrote:
> Hi,
>
> Is there any way to compare two dates in the Django template language?

There is not comparison (beyond equals / not equals) in the template
language at all. Even "ifchanged" is just an equality comparison spread
over a wide area. This is one of those "blurry dividing line" problems:
where do we stop putting the entire programming language in the
template. You just stepped on the line. :-)

> For example, I'd like to if the date variable due_date is before or
> after the current date. The timesince and timeuntil filters work, but
> they seem to require that I know ahead of time the order of the dates
> I'm comparing.

The normal approach is to add a property or a method, say past_due(), to
your object that returns a boolean based on whether the due_date is in
the past or not. Then

{% if object.past_due %}

will work intuitively.

Cheers,
Malcolm

Reply all
Reply to author
Forward
0 new messages