我在Django模板中设置了一个“ for / in”循环,并带有“ empty”选项,但是当我的视图没有任何项目时,我会得到404页面而不是“ empty”选项。
我尝试在“ if”标签中添加标签,但得到的结果相同。
模板代码:
{% for item in object_list %}
<p>{{ item.desc }}
{% empty %}
<p>Nothing scheduled
{% endfor %}
views.py:
class ItemTodayArchiveView(LoginRequiredMixin, TodayArchiveView):
login_url = '/admin/'
redirect_field_name= 'redirect_to'
date_field = 'airpub_date'
allow_future= True
def get_queryset(self):
destination = self.kwargs['destination']
return Item.objects.filter(airpub_date__gte=date.today()).filter(destination=destination)
当查询集为空时(即airpub_date没有任何内容),我希望模板页面显示“未安排任何内容”。相反,我得到一个404调试页面:
Page not found (404)
Request Method: GET
Request URL: http://xxx.xxx.xxx/items/atc/today/
Raised by: items.views.ItemTodayArchiveView
No items available