Boggio
Boggio

Reputation: 1148

Django template tag to show null date as "still open"

I have the following code in a Django template:

{{date_from|date:"Y-m"}} - {{date_to|default_if_none:"still open"|date:"Y-m"}}

I currently get:

  1. "2012-08 - "
  2. "2012-11 - 2012-08"
  3. .. rest of values that are correctly displayed.

I would like to get to this:

  1. "2012-08 - still open"
  2. "2012-11 - 2012-08"
  3. .. rest of values that are correctly displayed.

Do you have any suggestions? I think it is not displayed properly because it is a date object.

Upvotes: 2

Views: 937

Answers (1)

falsetru
falsetru

Reputation: 369364

Change the order of filters. Use default instead of default_if_none (date filter will return empty string for non-date/datetime object)

>>> t = Template('{{date_to|date:"Y-m"|default:"still open"}}')
>>> t.render(Context({'date_to': None}))
u'still open'
>>> t.render(Context({'date_to': datetime.datetime.now()}))
u'2014-04'

Upvotes: 4

Related Questions