GET方式
1 地址栏
2 form表单get方法
3 a标签<|a href="******">
4 js中的windows.location.href="******"
------------------------------------------------------------------------
原生分页
views.py
from django.shortcuts import render
# Create your views here.
from movie.models import Movie
import math
def page(num, size=20):
# GET进来的String强制转换为Int
num = int(num)
# 总记录数和总分页数
totalRecords = Movie.objects.count()
totalPages = int(math.ceil(totalRecords * 1.0 / size))
# 判断是否越界
if num < 1:
num = 1
if num > totalPages:
num = totalPages
# 每页切片分页
movies = Movie.objects.all()[((num - 1) * size):(num * size)]
return movies, num
def index_view(request):
# 接收分页请求参数
num = request.GET.get('num', 1)
# 处理分页请求
movies, n = page(num)
# 上一页的页码
pre_page_num = n - 1
# 下一页的页码
next_page_num = n + 1
return render(request, 'index01.html',
{
'movies': movies, 'pre_page_num': pre_page_num, 'next_page_num': next_page_num})
index01.html
<div>
<a href="/movie/?num={
{ pre_page_num }}">上一页</a>