问题描述
我正在使用Django过滤器,当我应用过滤器时,它们将起作用,但是当我转到结果的下一页时,它们将重置.我看过这篇文章 django-filter使用分页,但它们似乎在做相同的事情我是.我在做什么错了?
I am using Django filters and when I apply the filters they work but when I go to the next page of results they reset. I have looked at this post django-filter use paginations but they seem to be doing the same things that I am. What am I doing wrong?
url(r'^relations/$', views.annotation_views.relations, name="relations")
在应用过滤器时返回如下网址:
returns a url like this when the filters are applied:
/relations/?createdBy =& occursIn =& createdAfter =& createdBefore =& terminal_nodes =& project =
/relations/?createdBy=&occursIn=&createdAfter=&createdBefore=&terminal_nodes=&project=
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)
推荐答案
我能够通过复制url并将参数传递给模板来解决此问题.由于重复,我不得不删除页面参数.为此,我添加了以下内容:
I was able to solve this by copying the url and passing the params to the template. I had to delete the page param due to it being duplicated. In order to do this I added the following:
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 »</a>
</div>
{% endif %}
</div>
更新
通过直接获取过滤器对象的数据,我找到了一个更安全的解决方法
Update
I found a more secure fix for this by getting the data directly for the filter object
project = filtered.data.get('project')
然后将其添加到视图
&project={{ project }}
这篇关于转到下一页时,Django过滤器不适用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!