django 自定义分页 影片展示

django 自定义分页 影片展示_第1张图片

创建movie项目

创建模型类(逆向生成)

影片sql语句,直接拉到Navicat生成表 逆向生成模型类
http://note.youdao.com/noteshare?id=49e7e45a61fd266869ebacbc0134589b&sub=3F0F4155287749CF88AF0822060B78A9

python manage.py inspectdb>movie/models.py

配置URL

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url('admin/', admin.site.urls),
    url('movie/', include('movie.urls')),
]


from django.conf.urls import url
from movie import views

urlpatterns=[
    url(r'^$',views.index_view),

]


创建视图函数

from django.shortcuts import render
from movie.models import *
import math

def page(num=1,size=20):#每页显示
    num = int(num)
    if num < 1:
        num = 1

    # 计算总页数
    total_records = Movie.objects.count()
    total_pages = int(math.ceil(total_records * 1.0 / size))


    if num > total_pages:
        num = total_pages
    movielist = TMovie.objects.all() #获取所有数据类列表对象
    movies = movielist[((num - 1) * size):(num*size)]#列表切片


    return movies,num

def index_view(request):
 # 接收页面的get请求 并获得num值 如果没有就是1
    num = request.GET.get('num',1)
    num=int(num)
    
    pages_list,n = page(num)

    pre_page = n-1
    next_page = n+1

    conext={'movies': pages_list,'pre_page':pre_page,'next_page':next_page}
    return render(request,'movie.html',conext)


创建模板显示


 
    
    {% for movie in movies %}
  • {{ movie.name }}

    {{ movie.account}}
  • {% endfor %}

你可能感兴趣的:(django 自定义分页 影片展示)