django 笔记9 分页知识整理

感谢老男孩
自定义分页
    XSS:攻击 默认字符串返回

        {{page_str|safe}}  前端

        from django.utils.safestring import mark_safe
        page_str = """
            <a href="/user_list/?p=1">1</a>
                """
        page_str=mark_safe(page_str)  后端



        当前页:current_page
        总页数:total_count
        每页显示10条数据:per_page_count = 10
        页码: 11条

        如果:总页数 < 11
            start_index = 0
            end_index = 总页数
        else:  //总页数大于11
            当前页 <= 6
                start_index = 1
                end_index = 11

            else:
                start_index = 当前页-5
                end_index = 当前页+5 +1

                如果 当前 + 5 > 总页数:
                    end_index = 总页数 +1
                    start_index = 总页数 - 10

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]

    from django.utils.safestring import mark_safe
    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 = 12
        else:
            start_index = current_page -5
            end_index = current_page+5+1
            if (current_page+5) >total_count:
                start_index = total_count - 10
                end_index = total_count+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})
原文地址:https://www.cnblogs.com/Liang-jc/p/9196658.html