2017-08-29 108 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