最近学stm32单片机数据传输,Web端监控数据,由于数据比较多,查看不是很方便,因此需要安装一个分页插件来实现!
Django自带了一个 paginator
,使用起来不是很方便,所以可以使用 第三方的分页插件django-pure-pagination
!
下面就来介绍此插件的使用:
有两种方式安装,一种是直接通过 pip 安装,另外一种就是通过源码来安装了~~
在setting.py中设置:
INSTALLED_APPS = (
...
'pure_pagination',
)
PAGINATION_SETTINGS = {
'PAGE_RANGE_DISPLAYED': 10,
'MARGIN_PAGES_DISPLAYED': 2,
'SHOW_FIRST_PAGE_WHEN_INVALID': True,
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用来一页一页的显示
<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>
最终效果如图: