• [django]drf知识点梳理-分页


    msyql分页 limit offset

    https://www.cnblogs.com/iiiiiher/articles/8846194.html

    django自己实现分页

    https://www.cnblogs.com/iiiiiher/articles/9219935.html

    def index(request):
        # 分页算法
        # 1  0-10
        # 2  10-20
        per_page = 10
        cur_page = int(request.GET.get("p"))
        start = (cur_page - 1) * per_page
        end = cur_page * per_page
    
        # 页码
        page_num = len(user_list) // per_page
    
        ##上一页下一页
        if cur_page > 1:
            prev_page = cur_page - 1
        else:
            prev_page = 1
        if cur_page < page_num:
            next_page = cur_page + 1
        else:
            next_page = page_num
        return render(request, 'app01/index.html',
                      {'user_list': user_list[start:end], 'page_num': range(1, page_num + 1), 'prev_page': prev_page,
                       'next_page': next_page})
    

    django自带的Pagination

    https://blog.csdn.net/Yort2016/article/details/73551274

    def index(request):
        print(request.path)  # /app01/
        print(request.get_full_path())  # /app01/?name=maotai&age=22
    
        # for i in range(10):
        #     Article.objects.create(title='t%s' % i, content='content%s' % i)
        # print(Article.objects.all()[1:5].query.__str__())
        all_article = Article.objects.all()
    
        paginator = Paginator(all_article, 2)
        page = request.GET.get('page', '1')  # 默认让显示第一页
        page_obj = paginator.page(page)
    
        print(page_obj)
        return render(request, 'app01/index.html', {'page_obj': page_obj})
    
    index.html
    
    <div>
        {% for foo in page_obj %}
            <li>{{ foo }}</li>
        {% endfor %}
    
    </div>
    <div class="pagination">
                <span class="page-links">
                    {% if page_obj.has_previous %}
                        <a href="{{ request.path }}?page={{ page_obj.previous_page_number }}">previous</a>
                    {% endif %}
                    <span class="page-current">
                        Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                    </span>
                    {% if page_obj.has_next %}
                        <a href="{{ request.path }}?page={{ page_obj.next_page_number }}">next</a>
                    {% endif %}
                </span>
    </div>
    

    rest_framework的分页Pagination配置

    配置分页-使用系统自带的分页类

    全局配置: 给所有的model都分页

    https://www.django-rest-framework.org/api-guide/pagination/

    rest_framework里settings里教你怎么配

    • settings里设置rest_framework模块的配置

    - rest_framework的pagination.py
    
    class PageNumberPagination(BasePagination):
        """
        A simple page number based style that supports page numbers as
        query parameters. For example:
    
        http://api.example.org/accounts/?page=4
        http://api.example.org/accounts/?page=4&page_size=100
    
    
    class LimitOffsetPagination(BasePagination):
        """
        A limit/offset based style. For example:
    
        http://api.example.org/accounts/?limit=100
        http://api.example.org/accounts/?offset=400&limit=100
        ...
    

    设置自己的settings.py

    REST_FRAMEWORK = {
        'PAGE_SIZE': 1,
        'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination'
    }
    

    配置分页,使用自己的类

    - utils.pagination.Pagination.py
    
    from rest_framework.pagination import PageNumberPagination
    
    class Pagination(PageNumberPagination):
        def get_page_size(self, request):
            try:
                print(request.query_params) # <QueryDict: {'page': ['1'], 'page_size': ['-10']}>
                page_size = int(request.query_params.get("page_size", -1))
                # 如果前端传来的page_size小于0, 则取settings.py的PAGE_SIZE的值
                if page_size < 0:
                    return self.page_size # 这个是settings.py里的PAGE_SIZE的值
                # 如果前端传来的page_size大于0, 则用前端传来的
                return page_size
            except:
                pass
            return self.page_size
    
    - settings.py里的配置
    
    REST_FRAMEWORK = {
        'PAGE_SIZE': 1,
        # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
        'DEFAULT_PAGINATION_CLASS': 'utils.pagination.Pagination'
    }
    

    如果全局中某一个model不想分页咋办?

    views.py里设置下
    
        pagination_class = None
    

    仅仅想给Users模型分页

    - settings.py里配置
    
    REST_FRAMEWORK = {
        'PAGE_SIZE': 10,
        # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination'
    }
    
    - Users/views.py
    
    
    from .pagination import Pagination
    
    class UserViewset(viewsets.ReadOnlyModelViewSet):
        '''
        List:
            查询用户列表
        Retrieve:
            查询某用户详情
        '''
        queryset = user.objects.all()
        serializer_class = UserSerialiser
        pagination_class = Pagination
    

    分页效果及 分页存在的问题

  • 相关阅读:
    一份面试题+整理的答案
    java获取本机IPV4地址,非127.0.0.1
    List去重
    禁止iframe页面时产生历史记录
    js打开新窗口并且不被拦截
    ifream 跨域实现高度自适应
    css版tooltip
    jQuery遮罩插件 jQuery.blockUI.js
    SpringMVC 传递相同名称的参数
    SpringMVC接收List型参数
  • 原文地址:https://www.cnblogs.com/iiiiiher/p/9922734.html
Copyright © 2020-2023  润新知