代码之家  ›  专栏  ›  技术社区  ›  Kai

带有MonthArchiveView的Url给我错误404

  •  0
  • Kai  · 技术社区  · 7 年前

    我决定使用泛型类MonthArchiveView。

    主页的url和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:

    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 回复  |  直到 7 年前
        1
  •  1
  •   Alasdair    7 年前

    如果您没有该月的任何预订,则月份存档视图将返回404。您可以通过设置 allow_empty True .

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