步骤
- 构建models.py,通过mongoengine读取数据库,定义ArtiInfo类
from django.db import models
from mongoengine import *
# Create your models here.
class ArtiInfo(Document):
price = StringField()
title = StringField()
pub_date = StringField()
cates = ListField(StringField())
look = StringField()
area = ListField(StringField())
url = StringField()
meta = {'collection':'sample'}
- 在views中使用paginator库
from django.core.paginator import Paginator
from pure_layout_blog.models import ArtiInfo
- 在views.py中使用paginator
def index2(request):
limit = 4
arti_info = ArtiInfo.objects
paginator = Paginator(arti_info,limit) #按limit数进行分页
page = request.GET.get('page',1) #get URL中的page参数
loaded = paginator.page(page)
context = {
'ArtiInfo':loaded #页面中使用ArtiInfo作为当前页的对象
}
return render(request,'index2.html',context)
- 插入html代码
<div align="middle">
{% if ArtiInfo.has_previous %}
<a href="?page={{ ArtiInfo.previous_page_number }}"> 《《《Pre </a>
{% endif %}
<span>{{ ArtiInfo.number }} of {{ ArtiInfo.paginator.num_pages }} </span>
{% if ArtiInfo.has_next %}
<a href="?page={{ ArtiInfo.next_page_number }}">Next》》》</a>
{% endif %}
</div>