django执行数据库查询之后返回分页的结果

分页:

page = int(page) + 1
try:
    res = models.tables.objects.all().order_by('-id')

    paginator = Paginator(res, page_size)  # 生成分页实例
    dic['totalCount'] = paginator.count  # 获取数据总条数
    try:
        page_res = paginator.page(page)
    except PageNotAnInteger:
        page_res = paginator.page(1)
    except EmptyPage:
        page_res = paginator.page(1)
    L = []
    for p in page_res:
        b = model_to_dict(p)
        L.append(b)
    dic['code'] = '1'
    dic['message'] = ''
    dic['result'] = L
    return HttpResponse(json.dumps(dic, ensure_ascii=False))

page_size:接口传递参数,就是每页多少条

猜你喜欢

转载自blog.csdn.net/xopqaaa/article/details/88245855