zoukankan      html  css  js  c++  java
  • Django 学习笔记之七 实现分页

    接着上篇,在上篇的基础上实现网页数据分页显示

    1、打开views.py,编辑如下

    #coding:utf-8
    from django.shortcuts import render,get_object_or_404
    from blog.models import Post
    from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
    # Create your views here.
    def post_list(request):
    object_list = Post.objects.all()
    paginator = Paginator(object_list,3)#分页显示,每页3条记录
    page = request.GET.get('page')
    try:
    posts = paginator.page(page)
    except PageNotAnInteger:
    posts = paginator.page(1)
    except EmptyPage:
    posts = paginator.page(paginator.num_pages)
    return render(request,'blog/post/list.html',{'page':page,'posts':posts})

    def post_detail(request,post):
    post = get_object_or_404(Post,slug=post)
    # post = Post.objects.get(slug=post)
    return render(request,'blog/post/detail.html',{'post':post})

    2、打开urls.py,编辑如下

    from django.conf.urls import url,include
    from django.contrib import admin
    from blog.views import *


    urlpatterns = [
    # url(r'^blog/',blog_views.post_list,name='post_list'),
    url(r'^blog/', post_list, name='post_list'),
    url(r'^(?P<post>[-w]+)/$',
    post_detail, name='post_detail'),
    url(r'^(?P<post_id>)/share/$',post_share,name='post_share'),
    url(r'^admin/', admin.site.urls),
    ]


    3、建立html网页

      在 blog目录下建立templates文件,在下面再建立文件夹 blog,在下面增加 base.html,在blog文件夹下建立post文件夹,在此文件夹建立list.html文件和

         pagination.html。目录下:

      

      

      base.html文件内容如下:

      

       

    list.html 文件内容如下:


    pagination.html文件内容如下:
     

      

    4、在浏览器里输入:http://127.0.0.1:8000/blog/

    
    

    
    

    点击“Next”

      这样就实现了简单的分页功能

    
    
  • 相关阅读:
    【AtCoder】ARC075
    【BZOJ】3022: [Balkan2012]The Best Teams
    【Codeforces】Gym100633 D. LWDB
    MIME协议在邮件中的应用详解
    struts返回json数据
    mysql-存储过程(转载)
    安卓OKhttp请求封装
    安卓动态添加碎片
    通过163smtp服务器向各大邮箱发送邮件(SOCKET编程)
    安卓原生与hml交互(WebView基础)
  • 原文地址:https://www.cnblogs.com/shaosks/p/6206860.html
Copyright © 2011-2022 走看看