简体   繁体   中英

Url with MonthArchiveView give me error 404

I want to create a site (Django 1.8) where I can select events by date, exactly what happened in a given month and year. I decided to use the generic class MonthArchiveView.

The url to the main page passes the year and month, as is the url - but I get a 404 error

My view:

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

My url:

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

My link with parameters hardcoded):

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

If you do not have any bookings for that month, then the month archive view will return 404. You can change this behaviour by setting allow_empty to True .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM