开发者

Check if a date is in the future

开发者 https://www.devze.com 2023-01-12 11:15 出处:网络
How can I check from within a Django template whether a given date is in the future? Something like: {% if event.dat开发者_如何学Pythone > now %}

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()
0

精彩评论

暂无评论...
验证码 换一张
取 消