CBV覆盖django-filter上下文

时间:2017-02-22 23:14:42

标签: django django-filter

我正在尝试在我的项目中实施django-filters,但我遇到了障碍:/某种方式filter instance已从上下文中移除..

class ListReservations(LoginRequiredMixin, FilterView):
    template_name = 'reservations/homepage.html'
    paginate_by = 25
    model = Reservation
    filterset_class = ReservationFilter

    def get_context_data(self, **kwargs):
        ctx = super(ListReservations, self).get_context_data()
        ctx['today'] = datetime.datetime.now().strftime('%d/%m')
        return ctx

结果仍在过滤,但无法访问{{filter.form}}

2 个答案:

答案 0 :(得分:1)

对get_context_data的超级调用应该包括kwargs:

ctx = super(ListReservations, self).get_context_data(**kwargs)

答案 1 :(得分:0)

通过执行以下操作来实现它:

def get_context_data(self, **kwargs):
    ctx = super(ListReservations, self).get_context_data()
    ctx['today'] = datetime.datetime.now().strftime('%d/%m')
    ctx['filter'] = self.get_filterset(self.get_filterset_class())
    return ctx

希望有一天能帮助某人:)