在django应用程序的地址栏中显示日期

时间:2017-09-06 07:42:29

标签: python django date url view

我在django 1.8中有一个应用程序。我想在进入页面后立即查看特定日期的过滤结果,例如:

archive/2011/12

目前我有archive/2011/12的过滤结果,但是在地址栏中手动输入日期之后,当您点击链接并转到此视图时,如何自动输入日期。 我会感激任何提示。

class ArchiveListView(ListView, FormView, MonthArchiveView):
    model = models.Booking
    queryset = models.Booking.objects.order_by('-date_start')
    min_year = queryset.aggregate(Min('date_start'))
    max_year = queryset.aggregate(Max('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):
        min_year = self.queryset.aggregate(Min('date_start'))
        max_year = self.queryset.aggregate(Max('date_start'))
        context = super(ArchiveListView, self).get_context_data(**kwargs)
        context['mode'] = 'archive'
        context['year'] = range(min_year['date_start__min'].year, max_year['date_start__max'].year + 1)
        context['months'] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', ]
        context['selected_year'] = self.kwargs['year']
        context['selected_month'] = self.kwargs['month']
        context['object_list'] = models.Booking.objects.filter(date_start__year=self.get_year(),
                                                               date_start__month=self.get_month())
        print(context['selected_year'])
        return context

我的网址:

url(r'^/(?P<year>[0-9]{4})/(?P<month>[0-9]+)$', views.ArchiveListView.as_view(), name="archives"),

0 个答案:

没有答案