美文网首页
基于restframework分页(前后端备忘)

基于restframework分页(前后端备忘)

作者: lk_erzanml | 来源:发表于2021-01-27 22:51 被阅读0次
    # views里面定义分页器:
    
    class MyPageNumberPagination(PageNumberPagination):#商家分页器
        page_size = 10
        max_page_size = 10
        page_size_query_param = 'size'
        page_query_param = 'page'
    

    方法里面使用分页器:

    really_all = CompanyZhiWei.objects.get_queryset().order_by("-job_releasedate")
        page = MyPageNumberPagination()
        page_roles = page.paginate_queryset(queryset=really_all, request=request)
        serializer_data = ZhiWeiSerializer(page_roles, many=True, context={'request': request})
        reg1 = re.compile("\d")
        for i in serializer_data.data:
            res3 = reg1.findall(i['job_money'])
            if len(res3) != 0:
                i['job_money'] = i['job_money'] + "K"
        cnmd={}
        cnmd["count"]=page.page.paginator.count
        cnmd['next']=page.get_next_link()
        cnmd['previous']=page.get_previous_link()
        cnmd['data']=serializer_data.data
        cnmd['links']=page.get_html_context()
        cnmd['number']=page.page.number
        cnmd['allnumber']=page.page.paginator.num_pages
        return render(request, 'jobs.html',{"cnmd":cnmd})
    

    前端控制:

    {% if cnmd.previous %}
                <a href="{{ cnmd.previous }}" class="layui-laypage-prev"><em>←</em></a>
            {% else %}
                <a href="javascript:;" class="layui-laypage-prev"><em>←</em></a>
            {% endif %}
    
            {% for i in cnmd.links.page_links %}
                    {% if i.is_active %}
                    <a href="{{ i.url }}" style="color:#ff51d6;">{{ i.number|default:"..."}}</a>
                    {% else %}
                    <a href="{{ i.url }}">{{ i.number|default:"..."}}</a>
                    {% endif %}
    
            {% endfor %}
        
            {% if cnmd.next %}
                <a href="{{ cnmd.next }}" class="layui-laypage-next"><em>→</em></a>
            {% else %}
                <a href="javascript:;" class="layui-laypage-next"><em>→</em></a>
            {% endif %}
    

    相关文章

      网友评论

          本文标题:基于restframework分页(前后端备忘)

          本文链接:https://www.haomeiwen.com/subject/bcftzktx.html