基于这个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"
]
}
本来期待通过访问http://localhost/test/?page=2
得到“乔治”和“林戈”,但还是得到了“约翰”和“保罗”...
为什么?我错过什么了?
2条答案
按热度按时间rdrgkggo1#
1
是硬编码的。因此,第一页被获取。使用传入的
page
参数:3qpi33ja2#
在我的例子中,我在查询参数中为页码传递了错误的参数。它是
page
,但我正在通过page_no