2017-08-29 109 views
0

我想创建一个网站(Django的1.8),我可以按日期选择事件,确切发生在给定的月份和年份。 我决定使用通用类MonthArchiveView。Url与MonthArchiveView给我错误404

的URL主页通过的年份和月份,由于是网址 - 但我得到一个404错误

我的观点:

class BookingListView(ListView, MonthArchiveView): 
    model = models.Booking 
    queryset = models.Booking.objects.order_by('-date_start') 
    paginate_by = 80 
    template_name = 'events/archive_list.html' 
    context_object_name = 'object_list' 
    date_field = 'date_start' 
    allow_future = True 

    def get_context_data(self, **kwargs): 
     context = super(BookingListView, self).get_context_data(**kwargs) 
     context['mode'] = 'archive' 
     return context 

我的网址:

url(r'^(?P<year>[0-9]{4})/(?P<month>\d{2})/$', views.BookingListView.as_view(), name="list"), 

我的硬编码参数链接):

<a href="{% url 'archive:list' 2017 09 %}" title="{% trans 'Archive' %}"> 
{% trans 'Archive' %}#} 
</a> 

回答

1

如果您当月没有任何预订,那么月份归档视图将返回404.您可以通过将allow_empty设置为True来更改此行为。

class BookingListView(ListView, MonthArchiveView): 
    model = models.Booking 
    allow_empty = True 
    ... 
+0

非常感谢:) – Kai