我知道我可以str在 Django 模板中獲取當(dāng)前日期(使用模板標(biāo)簽now),如下所示:{% now "Y-m-d" as today_str %}<p>{{ today_str }}</p>但我不能將其用于比較:{% now "Y-m-d" as today_str %}{% for elem in object_list %} {% if elem.date < today_str %} {# WRONG: this compares 'date' and 'str' #} <p>{{ elem.pk }} before today</p> {# do some other rendering #} {% endif %}{% endfor %}可能的解決方案:我知道我可以將上下文變量傳遞給模板,但在我看來它需要代碼:# in my class-based-view in 'views.py'def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) ctx['today'] = timezone.now() return ctx或者我可以創(chuàng)建一個(gè)自定義模板標(biāo)簽,但這是更多的附加代碼。正如你所看到的,我有解決我的問題的方法,但我想知道是否有一種內(nèi)置方式來獲取模板中的當(dāng)前date(或datetime)?