Python实战计划——第四周第二节:分页

首先是新装了一个库pip3 install mongoengine

  • settings.py 链接mongodb中的ganji库
from mongoengine import connect
connect('ganji', host='127.0.0.1', port=27017)
  • modles.py 建立一个类,以便实例化,进行对象操作
from django.db import models
from mongoengine import *
#from mongoengine import connect
#connect('ganji', host='127.0.0.1', port=27017)

class Ershou_Info(Document):
    title = StringField()
    cates = ListField(StringField())
    price = StringField()
    pub_date = StringField()
    url = StringField()
    area = ListField(StringField())
    time = StringField()
    look = StringField()

    meta = {'collection': 'item_info'}#item_info中的key在类中要声明全部
# for i in Ershou_Info.objects[:10]:
#     print(i.title,i.price,i.area)
  • views.py
from django.shortcuts import render
from Django_blog.models import Ershou_Info
from django.core.paginator import Paginator
def blog(request):
    limit = 4
    ershou_info = Ershou_Info.objects[:20] # 将ershou_info实例化,下一行也是将paginatior分页实例化
    paginatior = Paginator(ershou_info, limit)  # 将参数一的东西,按参数二分;参数一必须是连续的
    page = request.GET.get('page', 1)
   # print(request)
   # print(request.GET)
    loaded = paginatior.page(page)
    context = {
        'Ershou_Info': loaded
    }
    return render(request, 'blog.html', context)

Python实战计划——第四周第二节:分页_第1张图片
运行结果.jpg

你可能感兴趣的:(Python实战计划——第四周第二节:分页)