Django Templates: Display a date only if it's in the future

妖精的绣舞 提交于 2019-12-04 18:50:24

I'd go with a template filter:

from datetime import date
...
@register.filter
def future_dates_only(the_date):
   if the_date > date.today():
       return the_date
   else:
       return None

Then in your view do something like:

{{specialdate|future_dates_only|date:"d M Y"}}

IMO it is cleaner to do that kind of things in a view or in a helper module, and pass it in the context. The templates are better left with no logic or the very least possible (that's the MVC pattern after all).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!