在Django的FilterView类中同时添加Django_filter和分页

时间:2018-12-29 15:59:22

标签: django filter pagination

我正在使用django_filters库来过滤列表视图,这是过滤器形式:

class WorkerFilter(django_filters.FilterSet):
    class Meta:
        model = Worker
        fields = ['id', 'branch']

视图的外观如下:

class WorkerListView(FilterView):
    model = Worker
    paginate_by = 1
    filter_class = WorkerFilter
    template_name = 'erp_system/worker_list.html'
    filterset_fields = ['id', 'branch']

    def get_queryset(self):
        new_context = Worker.objects.filter(
            active=True,
        )
        return new_context

并且正在以这种方式在HTML模板中使用表单:

   <form action="" method="get">
    {{ filter.form.as_p}}
        <button class="btn btn-warning" type="submit"><span class="fa fa-search"></span> بحث </button>
</form>

在此HTML页面的末尾,我有这样的列表和分页器:

        {% for item in filter.qs %}
{{item}}<br>
{% endfor %}
        {% if is_paginated %}
        <div class="pagination">
            <span class="page-links">
                {% if page_obj.has_previous %}
                    <a href="?page={{ page_obj.previous_page_number }}"><button class="btn-success">الصفحة السابقة</button> </a>
                {% endif %}
                <span class="page-current">
                     صفحة رقم  {{ page_obj.number }} من {{ page_obj.paginator.num_pages }}.
                </span>
                {% if page_obj.has_next %}
                    <a href="?page={{ page_obj.next_page_number }}"><button class="btn-success">الصفحة التالية</button> </a>
                {% endif %}
            </span>
        </div>
    {% endif %}

即使我使用paginate_by = 1进行调试并确保{% if is_paginated %}正常工作,分页器也不显示

0 个答案:

没有答案