python(django 网站分页)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .pagination .page{
            display: inline-block;
            padding: 5px;
            background-color: antiquewhite;
            margin: 5px ;
        }

    </style>
</head>
<body>
<ul>
    {% for item in li %}
        {% include 'li.html' %}
    {% endfor %}

    <div class="pagination">
    {{ page_str|safe }}
    </div>

</ul>


</body>
</html>
def user_list(request):
    current_page = request.GET.get('p',1)
    current_page = int(current_page)
    #(current_page-1)*10
    #current_page  * 10

    start = (current_page-1)*10
    end = current_page * 10
    data = LIST[start:end]
    all_count = len(LIST)
    count,y = divmod(all_count,10)
    if y:
        count += 1

    page_list = []
    for i in range(1,count+1):
        if i ==current_page:
            temp = '<a class="page_active" href="/user_list/?p=%s">%s</a>' %(i,i)
        else:
            temp = '<a class="page" href="/user_list/?p=%s">%s</a>' %(i,i)

        page_list.append(temp)




    page_str= "".join(page_list)
    page_str = mark_safe(page_str)
    return render(request,'user_list.html',{'li':data,'page_str':page_str})

 

分页2

 

def user_list(request):
    current_page = request.GET.get('p',1)
    current_page = int(current_page)

    per_page_count = 5

    #(current_page-1)*10
    #current_page  * 10

    start = (current_page-1)*per_page_count
    end = current_page * per_page_count
    data = LIST[start:end]
    all_count = len(LIST)
    count,y = divmod(all_count,per_page_count)
    if y:
        count += 1

    page_list = []
    for i in range(1,count+1):
        if i ==current_page:
            temp = '<a class="page_active" href="/user_list/?p=%s">%s</a>' %(i,i)
        else:
            temp = '<a class="page" href="/user_list/?p=%s">%s</a>' %(i,i)

        page_list.append(temp)




    page_str= "".join(page_list)
    page_str = mark_safe(page_str)
    return render(request,'user_list.html',{'li':data,'page_str':page_str})

模板3

def user_list(request):
    current_page = request.GET.get('p',1)
    current_page = int(current_page)

    per_page_count = 5

    #(current_page-1)*10
    #current_page  * 10

    start = (current_page-1)*per_page_count
    end = current_page * per_page_count
    data = LIST[start:end]
    all_count = len(LIST)
    total_count,y = divmod(all_count,per_page_count)
    if y:
        total_count += 1

    page_list = []
    # start_index = 1
    # end_index = count+1
    start_index = current_page - 5
    end_index = current_page + 5 +1
    if total_count<11:
        start_index =1
        end_index =total_count+1
    else:
        if current_page<=6:
            start_index = 1
            end_index = 11
        else:
            start_index = current_page - 5
            end_index = current_page +5 +1




    for i in range(start_index, end_index):
        if i ==current_page:
            temp = '<a class="page_active" href="/user_list/?p=%s">%s</a>' %(i,i)
        else:
            temp = '<a class="page" href="/user_list/?p=%s">%s</a>' %(i,i)

        page_list.append(temp)




    page_str= "".join(page_list)
    page_str = mark_safe(page_str)
    return render(request,'user_list.html',{'li':data,'page_str':page_str})
def user_list(request):
    current_page = request.GET.get('p',1)
    current_page = int(current_page)

    per_page_count = 5

    #(current_page-1)*10
    #current_page  * 10

    start = (current_page-1)*per_page_count
    end = current_page * per_page_count
    data = LIST[start:end]
    all_count = len(LIST)
    total_count,y = divmod(all_count,per_page_count)
    if y:
        total_count += 1

    page_list = []
    
    # start_index = 1
    # end_index = count+1
    start_index = current_page - 5
    end_index = current_page + 5 +1
    if total_count<11:
        start_index =1
        end_index =total_count+1
    else:
        if current_page<=6:
            start_index = 1
            end_index = 11
        else:
            start_index = current_page - 5
            end_index = current_page +5 +1
            if (current_page + 5) > total_count:
                end_index = total_count+1
                total_count = total_count - 11




    for i in range(start_index, end_index):
        if i ==current_page:
            temp = '<a class="page_active" href="/user_list/?p=%s">%s</a>' %(i,i)
        else:
            temp = '<a class="page" href="/user_list/?p=%s">%s</a>' %(i,i)

        page_list.append(temp)




    page_str= "".join(page_list)
    page_str = mark_safe(page_str)
    return render(request,'user_list.html',{'li':data,'page_str':page_str})

模板:4

def user_list(request):
    current_page = request.GET.get('p', 1)
    current_page = int(current_page)

    per_page_count = 5

    # (current_page-1)*10
    # current_page  * 10

    start = (current_page - 1) * per_page_count
    end = current_page * per_page_count
    data = LIST[start:end]
    all_count = len(LIST)
    total_count, y = divmod(all_count, per_page_count)
    if y:
        total_count += 1

    page_list = []
    pager_num = 11

    # start_index = 1
    # end_index = count+1
    start_index = current_page - 5
    end_index = current_page + 5 + 1
    if total_count < 11:
        start_index = 1
        end_index = total_count + 1
    else:
        if current_page <= (pager_num + 1) / 2:
            start_index = 1
            end_index = 11
        else:
            start_index = current_page - (pager_num - 1) / 2
            end_index = current_page + (pager_num + 1) / 2
            if (current_page + (pager_num - 1) / 2) > total_count:
                end_index = total_count + 1
                total_count = total_count - pager_num + 1
    if current_page ==1:
        prev = '<a class="page" href="javascript:void(0)">上一页</a>'
    else:

        prev = '<a class="page" href="/user_list/?p=%s">上一页</a>' % (current_page - 1,)
    page_list.append(prev)


    for i in range(int(start_index), int(end_index)):
        if i == current_page:
            temp = '<a class="page_active" href="/user_list/?p=%s">%s</a>' % (i, i)
        else:
            temp = '<a class="page" href="/user_list/?p=%s">%s</a>' % (i, i)

        page_list.append(temp)


    if current_page == total_count:
        nex = '<a class="page" href="javascript:void(0)">下一页</a>'
    else:
        nex = '<a class="page" href="/user_list/?p=%s">下一页</a>' % (current_page +1,)
    page_list.append(nex)


    jump = """
            <input type='text'  /><a onclick='jumpTo(this, "/user_list/?p=");'>GO</a>
            <script>
                function jumpTo(ths,base){
                    var val = ths.previousSibling.value;
                    location.href = base + val;
                }
            </script>
            """


    page_list.append(jump)

    page_str = "".join(page_list)
    page_str = mark_safe(page_str)
    return render(request, 'user_list.html', {'li': data, 'page_str': page_str})

发表评论