转到下一页时,Django过滤器未应用

时间:2017-09-01 19:54:21

标签: python django

我正在使用Django过滤器,当我应用过滤器时,它们可以工作,但是当我转到下一页的结果时,它们会重置。我看过这篇文章django-filter use paginations,但他们似乎做的和我一样。我做错了什么?

地址

url(r'^relations/$', views.annotation_views.relations, name="relations")
当应用过滤器时,

会返回这样的网址:

/关系/ createdBy =安培; occursIn =安培; createdAfter =安培; createdBefore =安培; terminal_nodes =安培;项目=

Filters.py

class RelationSetFilter(django_filters.FilterSet):
    occursIn = django_filters.CharFilter('occursIn__uri', method='filter_occursIn')
    createdBefore = django_filters.DateTimeFilter('created', lookup_expr='lt')
    createdAfter = django_filters.DateTimeFilter('created', lookup_expr='gt')

    terminal_nodes = django_filters.CharFilter('terminal_nodes__uri')


    def filter_occursIn(self, queryset, name, value):
        if not value:
            return queryset
        return queryset.filter(Q(occursIn__uri=value) | Q(occursIn__part_of__uri=value) | Q(occursIn__part_of__part_of__uri=value))

    class Meta:
        model = RelationSet
        fields = ['createdBy', 'project', 'occursIn', 'terminal_nodes']

查看

def relations(request):
    from annotations.filters import RelationSetFilter

    qs = RelationSet.objects.all()
    filtered = RelationSetFilter(request.GET, queryset=qs)
    qs = filtered.qs
    for r in qs:
        print r.__dict__

    paginator = Paginator(qs, 40)
    page = request.GET.get('page')
    try:
        relations = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        relations = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        relations = paginator.page(paginator.num_pages)

    context = {
        'paginator': paginator,
        'relations': relations,
        'params': request.GET.urlencode(),
        'filter': filtered,
    }
    return render(request, 'annotations/relations.html', context)

1 个答案:

答案 0 :(得分:-1)

我能够通过复制url并将params传递给模板来解决这个问题。由于它被复制,我不得不删除页面参数。为此,我添加了以下内容:

查看

def relations(request):
    from annotations.filters import RelationSetFilter

    qs = RelationSet.objects.all()
    filtered = RelationSetFilter(request.GET, queryset=qs)
    qs = filtered.qs
    for r in qs:
        print r.__dict__

    paginator = Paginator(qs, 40)
    page = request.GET.get('page')

    gt = request.GET.copy()
    if 'page' in gt:
        del gt['page']

    try:
        relations = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        relations = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        relations = paginator.page(paginator.num_pages)

    context = {
        'paginator': paginator,
        'relations': relations,
        'params': urlencode(gt),
        'filter': filtered,
        }
    return render(request, 'annotations/relations.html', context)

模板

<div class="col-xs-4  clearfix text-center">
            {% if relations.has_next %}
            <div class="pull-right">
                <a href="?page={{ relations.next_page_number }}&{{ params }}">Next &raquo;</a>
            </div>
            {% endif %}
        </div>

更新

我通过直接获取过滤器对象的数据

找到了更安全的解决方法
project = filtered.data.get('project')

然后将其添加到视图

&project={{ project }}