Reputation: 1148
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:
I would like to get to this:
Do you have any suggestions? I think it is not displayed properly because it is a date object.
Upvotes: 2
Views: 937
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