# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render # Create your views here. #渲染主页面 # from post.models import Post from templates.models import Post from django.core.paginator import Paginator import math def queryAll(request,num=1): num = int(num) #获取所有帖子信息 postList = Post.objects.all().order_by('-created') #创建分页器对象 pageObj = Paginator(postList,1) #获取当前页的数据 perPageList = pageObj.page(num) #生成页码数列表 # 每页开始页码 begin = (num - int(math.ceil(10.0 / 2))) if begin < 1: begin = 1 # 每页结束页码 end = begin + 9 if end > pageObj.num_pages: end = pageObj.num_pages if end <= 10: begin = 1 else: begin = end - 9 pageList = range(begin, end + 1) return render(request,'index.html',{'postList':perPageList,'pageList':pageList,'currentNum':num}) #阅读全文功能 def detail(request,postid): postid = int(postid) #根据postid查询帖子的详情信息 post = Post.objects.get(id=postid) return render(request,'detail.html',{'post':post})