我在 Django / Python 中有一个事件日历,我试图让它自动不显示基于当前日期已经过去的事件。我正在使用的代码如下所示:
views.py
class HomeView(ListView):
paginate_by = 1
model = NewsLetter
template_name = 'home.html'
ordering = ['-post_date']
def events(self):
return Event.objects.order_by('-event_date')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['Today'] = timezone.now().date()
return context
事件.html
{% for event in view.events %}
<div class="py-2">
{% if event.date <= Today %}
<ul>
<li class="font-bold text-gray-900">{{ event.date }}</li>
<li class="font-medium text-gray-800">{{ event.name }}</li>
<li class="font-medium text-gray-800">{{ event.description }}</li>
<strong><p>Location:</p></strong>
<li class="font-medium text-gray-800">{{ event.location }}</li>
{% if event.website_url %}
<a class="font-medium text-gray-800 hover:font-bold hover:text-blue-600" href="{{ event.website_url }}"
target="blank">Information
</a>
{% endif %}
</ul>
{% endif %}
</div>
<hr>
{% endfor %}
森林海
相关分类