我有一个网站,它基本上显示了一个巨大的人表,我希望能够过滤、排序和分页。我想在多个视图中执行此操作,这些视图具有不同的预先存在的约束,人们在表中显示这些约束。我的问题是,如果我按状态过滤,例如按名称排序,然后尝试转到下一页,它会重置。例如,如果我过滤一次我得到 my/url/2013/?sort=n&state=MN&page=1; 然后如果我转到第 2 页,我只会得到 my/url/2013/?page=2。它不记得我已经问过什么了。
def all(request, year=default_year):
#sorting (sort_options was defined prior to this view)
if 'sort' in request.GET:
sort = request.GET.get('sort')
order = sort_options[sort]
else:
order = '-score'
players = Player.objects.filter(year=year).order_by(order)
url_query = ''
#filtering
if 'position' in request.GET:
filterData = _filter(request,players)
players = filterData['players']
url_query += filterData['url_query']
# pagination
paginator = Paginator(players,25)
page = request.GET.get('page')
try:
players = paginator.page(page)
except PageNotAnInteger:
players = paginator.page(1)
except EmptyPage:
players = paginator.page(paginator.num_pages)
data = {'players':players,'url_query':url_query}
return render_to_response('all.html', data, context_instance = RequestContext(request))
在我的模板中,我像这样引用 url_query:
<th class="name"><a href="?sort=n&{{url_query}}&page={{players.number}}">Name</a></th>
就像这样:
<a href="?{{url_query}}&page={{ players.next_page_number }}">Next</a>
这对我来说显然是错误的,但我不知道如何正确地做到这一点。