Django 数据自定义分页

XSS:
{{ page_str|safe }}

from django.utils.safestring import mark_safe


mark_safe(page_str)#保证回传数据安全的

 

前端页面:

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

        }
    </style>
</head>
<body>
    <ul>
        {% for  itme in li %}
            <li>{{ itme }}</li>

        {% endfor %}
    </ul>
    <div>
        <select id="ps" onchange="changePageSize(this)">
            <option value="10">10</option>
            <option value="30">30</option>
            <option value="50">50</option>
            <option value="100">100</option>
        </select>
    </div>
    <div class="pagination">
        {{ page_str }}
    </div>
</body>
</html>

 

python代码

from django.shortcuts import render,HttpResponse
# Create your views here.
from django.urls import reverse
from django.utils.safestring import mark_safe

LIST = []
for i in range(500):
    LIST.append(i)

def user_list(request):
    current_page = request.GET.get('p', 1)
    current_page = int(current_page)#获取当前页转换为int类型
    print('current_page',current_page)
    start = (current_page-1)*10
    end = current_page*10
    list = LIST[start:end]#通过列表切割

    all_count = len(LIST)#获取总数据
    count,y = divmod(all_count,10)#取商和余
    if y:#判断是否为真。0为假,其余为真
        count +=1

    page_list=[]
    page_num=5#显示多少个页码
    start_index= current_page - (page_num-1)/2
    end_index= current_page + (page_num*2)+1

    if count<11:
        start_index=1
        end_index=count+1
    else:
        if current_page<=(page_num-1)/2:
            start_index=1
            end_index =page_num+1
        else:
            start_index= current_page -(page_num-1)/2
            end_index = current_page +(page_num+1)/2
            if (current_page+(page_num-1)/2)>count:
                end_index = count +1
                start_index = count - page_num-1
    if current_page ==1:#判断等于1的时候不能上一页跳转
        prev = '<a class="page_list" href="javascript:void(0)">上一页</a>'
    else:
        prev ='<a class="page_list" href="/user/?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_list active"  href="/user/?p=%s">%s</a>'%(i,i)
        else:
            temp='<a class="page_list" href="/user/?p=%s">%s</a>'%(i,i)
        page_list.append(temp)
    if current_page ==count:#同上一页逻辑
        next_page = '<a class="page_list" href="javascript:void(0)">下一页</a>'
    else:
        next_page ='<a class="page_list" href="/user/?p=%s">下一页</a>'%(current_page+1,)
    page_list.append(next_page)

    jump="""
    <input type'text'/><a onclick='jumpTo(this, "/user/?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':list,'page_str':page_str})

 

posted @ 2021-05-12 23:24  安好_世界  阅读(57)  评论(0编辑  收藏  举报