Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django if now=模板中的发布日期_Django - Fatal编程技术网

Django if now=模板中的发布日期

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 %} 但即使它应该匹配,我在任

我有点迷路了,在我的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 %}
但即使它应该匹配,我在任何地方都被终结了。。。你知道吗

在我的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 %}