ListView和Djangofilter无法正常工作的Django分页

2024-04-27 02:23:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在ListView中使用django-filter,这会导致分页问题。在我的列表视图中,当页面加载时,分页显示正确并显示 底部1-7页的页面链接。但是,当我转到这些页面中的任何一个时,它会显示所有模型实例。例如,我单击第2页,url后面附加了?page=2,当我将paginate-by设置为20时,我希望对象的id为21-40。在

我知道这个问题是由django过滤器引起的,因为当我从“get_context_data”中删除它时,分页工作正常。我只是在用自我查询集'作为django filter对象中的一个参数,因此我无法找出发生这种情况的原因。分页的html包含在一个基本模板中,我在ListView子类的模板中扩展了这个基模板。在

如果我在上下文数据中包含django过滤器和/或为了修复它应该做些什么,对于为什么分页不能正常工作提出任何建议都将不胜感激。在

基本ListView和一个子类:

class BaseCompoundListView(ListView):
    queryset = Compound.objects.all()
    template_name = 'compounds/compound_list.html'
    paginate_by = 20        

    def get_context_data(self, **kwargs):
        context = super(BaseCompoundListView, self).get_context_data(**kwargs)
        context['odor_types'] = OdorType.objects.values('term')
        compound_filter = CompoundFilter(self.request.GET, queryset=self.queryset)
        context['compound_filter'] = compound_filter
        return context

class CompoundListView(BaseCompoundListView):

    def get_context_data(self, **kwargs):
        context = super(CompoundListView, self).get_context_data(**kwargs)
        context['page_header'] = 'All compounds'
        return context

基本模板:

^{pr2}$

Tags: djangoself模板datagetcontextpage页面