django给app返回接口列表, 流式分页
Posted Wyc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django给app返回接口列表, 流式分页相关的知识,希望对你有一定的参考价值。
# -*- coding: utf-8 -*- __author__ = ‘YongCong Wu‘ # @Time : 2019/12/30 12:45 # @Email : : 1922878025@qq.com class Pagenation(object): def __init__(self,data_list,page,per_data_num = 10): ‘‘‘ 初始化 :param data_list: 所有数据列表 :param page: 当前要查看的列表页 :param per_data_num: 每页默认要显示几条 ‘‘‘ self.data_list = data_list self.page = page self.per_data_num = per_data_num @property def start(self): ‘‘‘ 计算引索的起始位置 :return: ‘‘‘ return (self.page - 1) * self.per_data_num @property def end(self): ‘‘‘ 计算引索的结束位置 :return: ‘‘‘ return self.page * self.per_data_num def show(self): ‘‘‘ 切片取数据,展示对应分页的结果 :return: ‘‘‘ result = self.data_list[self.start:self.end] data = [] for row in result: data.append(row) return data
def articleNews(request, question_id): result = {} data = [] print(question_id) content_list = models.AriticeModel.objects.all() for i in content_list: data_dict = {} data_dict[‘image‘] = i.img_url data_dict[‘id‘] = i.id data_dict[‘post_title‘] = i.title data_dict[‘post_like‘] = 525 data_dict[‘category_name‘] = i.aritice_gjz data.append(data_dict) obj = pagetion.Pagenation(data, question_id) result[‘data‘] = obj.show() result[‘code‘] = 200 return JsonResponse(result)
以上是关于django给app返回接口列表, 流式分页的主要内容,如果未能解决你的问题,请参考以下文章