Django if now=模板中的发布日期
我有点迷路了,在我的MySQL数据库中,DateField是这样保存的2020-10-29 在我的模板中,我尝试查看DateField==Now。在我的模型中,它是DateField而不是DateTimeField 所以我试着这样做:Django if now=模板中的发布日期,django,Django,我有点迷路了,在我的MySQL数据库中,DateField是这样保存的2020-10-29 在我的模板中,我尝试查看DateField==Now。在我的模型中,它是DateField而不是DateTimeField 所以我试着这样做: {% now "Y-F-j" as current_date %} {% if post.start_date == current_date %} Today{% else %} Ended{% endif %} 但即使它应该匹配,我在任
{% now "Y-F-j" as current_date %}
{% if post.start_date == current_date %} Today{% else %} Ended{% endif %}
但即使它应该匹配,我在任何地方都被终结了。。。你知道吗
在我的MySQL数据库中,DateField是这样保存的2020-10-29
不,这只是它如何向外界展示它的格式。对于DateField
,您应该假设它保存了date
对象datetime
对象。不是一串
我建议将当天的内容传递给上下文:
from django.utils.timezone import now
def my_view(request):
# …
context = {
# …,
'today' : now().date()
}
return render(request, 'some_template.html', context)
请注意,Django提供了一个更方便的方法来呈现日期
(时间
)对象
{% if post.start_date == today %} Today{% else %} Ended{% endif %}