zoukankan      html  css  js  c++  java
  • 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})
  • 相关阅读:
    Conversions
    Mispelling4
    A hard puzzle
    Easier Done Than Said?
    利用map可以对很大的数出现的次数进行记数
    A+B Coming
    结构体成员变量
    NSString 类介绍及用法
    复习回顾
    函数与方法对比
  • 原文地址:https://www.cnblogs.com/anhao-world/p/14665026.html
Copyright © 2011-2022 走看看