You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Django模板中判断数据库中的日期是否逾期

解决Django模板中判断日期逾期的问题

问题核心

你的逻辑方向是对的,但模板中未传入today变量,且未处理due_date为空的情况,导致判断失效。

方案一:视图传递当前日期

在对应视图函数中,把当前日期传入模板上下文:

# views.py
from datetime import date
from django.shortcuts import render
from .models import Update

def index(request):
    updates = Update.objects.all()
    today = date.today()
    return render(request, 'index.html', {'updates': updates, 'today': today})

修改模板,先判断due_date是否存在,再做逾期判断:

{% if update.due_date %}
    {% if update.due_date <= today %}
        <td class="py-2 px-4 text-center">
            <span class="p-1 text-xs border rounded-md bg-red-100 border-red-400 text-red-600">Overdue</span>
        </td>
    {% else %}
        <td class="py-2 px-4 text-center">
            <span class="p-1 text-xs border rounded-md bg-green-100 border-green-400 text-green-600">Current</span>
        </td>
    {% endif %}
{% else %}
    <td class="py-2 px-4 text-center">
        <span class="p-1 text-xs border rounded-md bg-gray-100 border-gray-400 text-gray-600">No Due Date</span>
    </td>
{% endif %}

方案二:使用Django内置模板标签now

无需在视图传值,直接用模板标签获取当前日期:

{% load tz %} <!-- 需时区支持时加载此标签 -->
{% if update.due_date %}
    {% if update.due_date <= now|date:"Y-m-d" %}
        <td class="py-2 px-4 text-center">
            <span class="p-1 text-xs border rounded-md bg-red-100 border-red-400 text-red-600">Overdue</span>
        </td>
    {% else %}
        <td class="py-2 px-4 text-center">
            <span class="p-1 text-xs border rounded-md bg-green-100 border-green-400 text-green-600">Current</span>
        </td>
    {% endif %}
{% else %}
    <td class="py-2 px-4 text-center">
        <span class="p-1 text-xs border rounded-md bg-gray-100 border-gray-400 text-gray-600">No Due Date</span>
    </td>
{% endif %}

优化建议:模型添加属性方法

如果需要频繁判断逾期状态,可在模型中定义属性方法简化模板逻辑:

# models.py
from datetime import date

class Update(models.Model):
    date_created = models.DateField(blank=True, null=True)
    due_date = models.DateField(blank=True, null=True)

    @property
    def is_overdue(self):
        if not self.due_date:
            return False
        return self.due_date <= date.today()

模板中直接调用属性:

{% if update.due_date %}
    {% if update.is_overdue %}
        <td class="py-2 px-4 text-center">
            <span class="p-1 text-xs border rounded-md bg-red-100 border-red-400 text-red-600">Overdue</span>
        </td>
    {% else %}
        <td class="py-2 px-4 text-center">
            <span class="p-1 text-xs border rounded-md bg-green-100 border-green-400 text-green-600">Current</span>
        </td>
    {% endif %}
{% else %}
    <td class="py-2 px-4 text-center">
        <span class="p-1 text-xs border rounded-md bg-gray-100 border-gray-400 text-gray-600">No Due Date</span>
    </td>
{% endif %}

内容的提问来源于stack exchange,提问作者Willow6949

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 00:46:15