前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Django添加分页功能

Django添加分页功能

作者头像
緣來
发布2020-01-02 15:09:51
8490
发布2020-01-02 15:09:51
举报
文章被收录于专栏:緣來來來

最近学stm32单片机数据传输,Web端监控数据,由于数据比较多,查看不是很方便,因此需要安装一个分页插件来实现!

Django自带了一个 paginator ,使用起来不是很方便,所以可以使用 第三方的分页插件django-pure-pagination

下面就来介绍此插件的使用:

安装

有两种方式安装,一种是直接通过 pip 安装,另外一种就是通过源码来安装了~~

  • pip 安装 pip install django-pure-pagination
  • 源码安装 git clone https://github.com/jamespacileo/django-pure-pagination.git cd django-pure-pagination python setup.py install

配置

在setting.py中设置:

代码语言:javascript
复制
INSTALLED_APPS = (
    ...
    'pure_pagination',
)

PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED': 10,
    'MARGIN_PAGES_DISPLAYED': 2,

    'SHOW_FIRST_PAGE_WHEN_INVALID': True,

视图

代码语言:javascript
复制
from pure_pagination import Paginator, PageNotAnInteger

    ···

def data_history(request):
    datas=Data.objects.all()
    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    p = Paginator(datas, 8, request=request)
    data=p.page(page)
    return render(request,"index.html",{"data":data})

    ···

其中 datas 为从数据库中得到内容,data 为需要传到html中的内容。

前端

在for循环时,需要在data后多加个object_list用来一页一页的显示

代码语言:javascript
复制
<div class="row" >
  <div class="col-md-12" style="border-left:red 2px solid;">
    <table class="table table-striped">
     <caption>空气状况</caption>
     <thead>
       <tr>
          <th>温度</th>
         <th>湿度</th>
         <th>PH值</th>
          <th>经度</th>
         <th>纬度</th>
         <th>时间</th>
       </tr>
       </thead>
      <tbody>
     {% for  dataone in data.object_list %}
        <tr>
          <td>{{ dataone.temp }}</td>
         <td>{{ dataone.humi }}</td>
         <td>{{ dataone.ph }}</td>
         <td>{{ dataone.jingdu }}</td>
         <td>{{ dataone.weidu }}</td>
          <td>{{ dataone.time }}</td>
       </tr>
     {% endfor %}
      </tbody>
    </table>
  </div>
</div>


{# The following renders the pagination html #}
<div id="pagination" style="margin-top:-40px;">
    {{ data.render }}
</div>

最终效果如图:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-10-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装
  • 配置
  • 视图
  • 前端
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档