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

2021-01-27  本文已影响0人  lk_erzanml
# 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 %}
上一篇下一篇

猜你喜欢

热点阅读