基于此tutorial, 我试图对列表进行分页,但失败了。
views.py
@api_view(['GET'])
def test(request):
objects = ['john', 'paul', 'george', 'ringo']
paginator = Paginator(objects, 2)
page = paginator.page(1)
serializer = PaginationSerializer(instance=page, context={'request':request})
return Response(serializer.data)
urls.py
urlpatterns = patterns('', (r'^test/$', 'ptt.views.test'))
{
"count": 4,
"next": "http://localhost/test/?page=2",
"previous": null,
"results": [
"john",
"paul"
]
}
我期待得到"乔治"和" ringo"访问http://localhost/test/?page=2
,
但我还是得到了约翰"和"保罗" ...
为什么?我错过了什么?
答案 0 :(得分:1)
1
是硬编码的。所以第一页被提取。
page = paginator.page(1)
使用传递的page
参数:
page = paginator.page(int(request.GET.get('page', '1')))