在我的html模板中,我加载了一个对象列表,我现在正在尝试将Django-Pagination(来自文档)和order_by方法结合起来。
我试图放一些加载href =“/ mylist /?order_by = somefield
的排序链接它适用于第一页。但是当我点击“下一页”时,顺序似乎打破了。这有什么问题?
“下一页”链接加载href =“mylist /?page = {{results.next_page_number}}
查看:
def Mylist(request):
order_by = request.GET.get('order_by', 'somedefault')
myobjects_list = Mymodel.objects.filter(user=request.user).order_by(order_by)
paginator = Paginator(myobjects_list, 5)
page = request.GET.get('page')
try:
results = paginator.page(page)
except PageNotAnInteger:
results = paginator.page(1)
except EmptyPage:
results = paginator.page(paginator.num_pages)
context = {'results ': results }
return render_to_response('mylist.html', context, context_instance=RequestContext(request))
答案 0 :(得分:0)
在视图中:
context = {'results ': results, 'order_by': order_by}
在模板中:
href="mylist/?page={{ results.next_page_number }}&order_by={{ order_by }}"