Python实战计划学习笔记(18)分页器实战

步骤

  1. 构建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'}
  1. 在views中使用paginator库
from django.core.paginator import Paginator
from pure_layout_blog.models import ArtiInfo
  1. 在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)
  1. 插入html代码
            
{% if ArtiInfo.has_previous %} 《《《Pre {% endif %} {{ ArtiInfo.number }} of {{ ArtiInfo.paginator.num_pages }} {% if ArtiInfo.has_next %} Next》》》 {% endif %}

你可能感兴趣的:(Python实战计划学习笔记(18)分页器实战)