Check if a date is in the future
How can I check from within a Django template whether a given date is in the future?
Something like:
{% if event.dat开发者_如何学Pythone > now %}
Write a function on event named in_future
, that will compare event.date
with datetime.now()
and use it in the template. Don't put unnecessary logic into template.
Or as Manoj suggested, you can have a custom filter in_the_future
and call it:
{% if event.date|in_the_future %}
It's as simple as:
@register.filter
def in_the_future(value):
return value > datetime.now()
精彩评论