zoukankan      html  css  js  c++  java
  • django 分页

    django 分页,urls

    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    urlpatterns = [
        #url(r'^admin/', admin.site.urls),
        url(r'^index/(d*)/', views.index),
    ]

    views

    #!/usr/bin/env python
    #_*_coding:utf-8_*_
    
    from django.shortcuts import render,render_to_response,redirect,HttpResponse
    from app01 import models
    from django.utils.safestring import mark_safe
    import common
    # Create your views here.
    
    def index(request,page):
    
        pageint = 5
        page = common.try_int(page)
        startpage = (page-1) * pageint  #根据url传过来的参数判断开始条数
        endpage = page * pageint        #根据url传过来的参数判断结束条数
    
        hostall = models.Host.objects.all()[startpage:endpage]   #一页显示的条数
        hostcount= models.Host.objects.all().count()             #获取数据库的总共条数
    
        #判断总页数,如果能整除5,页数不加一, 不过不能整除5,需要加一
        tempcountpage = divmod(hostcount,pageint)
        if tempcountpage[1] == 0:
            all_page_count = tempcountpage[0]
        else:
            all_page_count = tempcountpage[0] +1
    
        #定义一个列表,然后循环总页数,追加到列表中,
        page_html = []
    
        #首页
        first_page = '<a href="/index/%d">首页</a>'%(1)
        page_html.append(first_page)
    
        #上一页
        if page <= 1:
            up_page = '<a href="/index/%d">上一页</a>'%(1)
        else:
            up_page = '<a href="/index/%d">上一页</a>'%(page - 1)
    
        page_html.append(up_page)
    
        #显示1。。。最后的页数
        for i in  range(all_page_count):
            if page == i +1:
                temp_html = '<a class="selected" href="/index/%d">[%d]</a>'%(i+1,i+1)
            else:
                temp_html = '<a href="/index/%d">[%d]</a>'%(i+1,i+1)
    
            page_html.append(temp_html)
    
        #下一页
        if page >= all_page_count:
            next_page = '<a href="/index/%d">[下一页]</a>' % (all_page_count)
        else:
            next_page = '<a href="/index/%d">[下一页]</a>'%(page + 1)
            page_html.append(next_page)
    
        # 尾页
        end_page = '<a href="/index/%d">[尾页]</a>' %(all_page_count)
        page_html.append(end_page)
    
    
        # 使用mark_safe 可让html显示返回的a标签。join 把列表取出用空格连接
        page_html = mark_safe(' '.join(page_html))
    
        ret = {'data':hostall,'count':hostcount,'page_ret':page_html}
        return render_to_response('index.html',ret)


    html,index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            body
            {
                font-size: .85em;
                font-family:"Trebuchet MS",Verdana,Helvetica,"Sakkal Majalla";
                color: #232323;
                background-color: #ffffff;
            }
            #Pageing A{ background-color:aliceblue;
                border: 1px solid white;
                text-decoration: none;
                color:black;
                padding: .1em .6em .2em .6em;}
    
            #Pageing A.selected{
                background-color: #AA7700;
            }
        </style>
    </head>
    <body>
    <table border="1px">
        <tr>
            <td>HostName</td>
            <td>IP</td>
        </tr>
        {% for item in data %}
        <tr>
            <td>{{ item.HostName }}</td>
            <td>{{ item.ip }}</td>
        </tr>
        {% endfor %}
    </table>
    
    <div style="color: black;font-size: medium"> 共计{{ count }}条数据</div>
    <div id="Pageing"> {{ page_ret }}</div>
    </body>
    </html>

    modles.py

    from __future__ import unicode_literals
    
    from django.db import models
    
    # Create your models here.
    class Host(models.Model):
        HostName = models.CharField(max_length=256)
        ip = models.GenericIPAddressField()
  • 相关阅读:
    libmv
    visualSFM
    opencv学习笔记——时间计算函数getTickCount()和getTickFrequency()
    opencv学习笔记——cv::mean()函数详解
    linux使用ip能ping通,但使用域名却不能访问的解决方法
    yum 安装出错--"Couldn't resolve host 'mirrors.aliyun.com'"
    vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0'
    VMWare虚拟机 网络连接模式
    js监听input输入框值的实时变化实例
    本地连接linux虚拟机的方法
  • 原文地址:https://www.cnblogs.com/fengjian2016/p/5359427.html
Copyright © 2011-2022 走看看