Django 1.4 - 跟踪多个查询和分页

时间:2013-04-07 06:51:29

标签: django

我有一个网站,基本上显示了一个巨大的人员表,我希望能够过滤,排序和翻页。我想在多个视图中执行此操作,这些视图具有不同的预先存在的约束,人们在表中显示。我的问题是,如果我按状态过滤,按名称排序,然后尝试转到下一页,它会重置。例如,如果我过滤了我的/ url / 2013 /?sort = n& state = MN& page = 1;然后,如果我转到第2页,我只是得到我的/ 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>

这对我来说显然是错误的,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

渲染模板时,您可以传递给上下文(在您的案例中为data)对象sort参数,然后在模板中您可以使用url来显示要调用的视图