Python Django 添加首页尾页上一页下一页代码实例

  • 时间:
  • 浏览:31

增加尾页战尾页:

views.py:

from django.shortcuts import render
from app01 import models
def book_list(request):
  # 从 URL 中与参数
  page_num = request.GET.get("page")
  print(page_num, type(page_num))
  page_num = int(page_num) 
  # 界说两个变量保留数据从哪女与到哪女
  data_start = (page_num - 1) * 10
  data_end = page_num * 10 
  # 册本总数
  total_count = models.Book.objects.all().count() 
  # 每页显现几条数据
  per_page = 10
  # 统共需求几页码去显现
  total_page, m = divmod(total_count, per_page) 
  # 页里上最多展现的页码
  max_page = 11
  half_max_page = max_page // 2
 
  # 页里上展现的页码的起头页
  page_start = page_num - half_max_page
  # 页里上展现的页码的完毕页
  page_end = page_num + half_max_page 
  # 若是以后页加1半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 若是以后页减1半比总页码借年夜
  if page_end > total_page:
    page_end = total_page
    page_start = total_page - max_page + 1 
  # 若是借无数据
  if m:
    total_page += 1 
  all_book = models.Book.objects.all()[data_start:data_end] 
  # 拼接 html 的分页代码
  html_list = [] 
  # 增加尾页按钮
  html_list.append('
  • 尾页
  • ') # 展现的页码 for i in range(page_start, page_end + 1): tmp = '
  • {0}
  • '.format(i) html_list.append(tmp) # 增加尾页按钮 html_list.append('
  • 尾页
  • '.format(total_page)) page_html = "".join(html_list) # 拼接 html 的分页代码 return render(request, "book_list.html", {"books": all_book, "page_html": page_html})

    book_list.html:

    
    
    
      
      册本列表
      
    
     
    
    {% for book in books %} {% endfor %}
    序号 id 书名
    {{ forloop.counter }} {{ book.id }} {{ book.title }}

    运转成果:

    增加上1页、下1页:

    views.py:

    from django.shortcuts import render
    from app01 import models 
    def book_list(request):
      # 从 URL 中与参数
      page_num = request.GET.get("page")
      print(page_num, type(page_num))
      page_num = int(page_num) 
      # 界说两个变量保留数据从哪女与到哪女
      data_start = (page_num - 1) * 10
      data_end = page_num * 10 
      # 册本总数
      total_count = models.Book.objects.all().count() 
      # 每页显现几条数据
      per_page = 10 
      # 统共需求几页码去显现
      total_page, m = divmod(total_count, per_page) 
      # 页里上最多展现的页码
      max_page = 11
      half_max_page = max_page // 2 
      # 页里上展现的页码的起头页
      page_start = page_num - half_max_page
      # 页里上展现的页码的完毕页
      page_end = page_num + half_max_page 
      # 若是以后页加1半比 1 小
      if page_start <= 1:
        page_start = 1
        page_end = max_page
      # 若是以后页减1半比总页码借年夜
      if page_end > total_page:
        page_end = total_page
        page_start = total_page - max_page + 1 
      # 若是借无数据
      if m:
        total_page += 1 
      all_book = models.Book.objects.all()[data_start:data_end] 
      # 拼接 html 的分页代码
      html_list = [] 
      # 增加尾页按钮
      html_list.append('
  • 尾页
  • ') # 若是是第1页,便出有上1页 if page_num <= 1: html_list.append('
  • '.format(page_num⑴)) else: # 减1个上1页的标签 html_list.append('
  • '.format(page_num⑴)) # 展现的页码 for i in range(page_start, page_end + 1): # 给以后页增加 active if i == page_num: tmp = '
  • {0}
  • '.format(i) else: tmp = '
  • {0}
  • '.format(i) html_list.append(tmp) # 若是是最初1页,便出有下1页 if page_num >= total_page: html_list.append('
  • ') else: html_list.append('
  • '.format(page_num+1)) # 增加尾页按钮 html_list.append('
  • 尾页
  • '.format(total_page)) page_html = "".join(html_list) # 拼接 html 的分页代码 return render(request, "book_list.html", {"books": all_book, "page_html": page_html})

    book_list.html:

    
    
    
      
      册本列表
      
    
     
    
    {% for book in books %} {% endfor %}
    序号 id 书名
    {{ forloop.counter }} {{ book.id }} {{ book.title }}

    运转成果:

    后绝改良:

    处置用户传给 url 的 page 参数非常的值的状况

    比方:

    拜候,http://127.0.0.1:8888/book_list/?page=a

    拜候,http://127.0.0.1:8888/book_list/?page=⑴

    城市堕落

    改良:

    from django.shortcuts import render
    from app01 import models
    def book_list(request):
      # 从 URL 中与参数
      page_num = request.GET.get("page")
      print(page_num, type(page_num)) # page_num 为 str 范例
      # 册本总数
      total_count = models.Book.objects.all().count() 
      # 每页显现几条数据
      per_page = 10 
      # 统共需求几页码去显现
      total_page, m = divmod(total_count, per_page) 
      # 若是借无数据
      if m:
        total_page += 1
      try:
        page_num = int(page_num)
        # 若是输出的页码数超越了最年夜的页码数,默许前往最初1页
        if page_num > total_page:
          page_num = total_page
        # 若是输出的页码数小于 1,则前往第1页
        if page_num < 1:
          page_num = 1
      except Exception as e:
        # 当输出的页码没有是端庄数字的时分 默许前往第1页的数据
        page_num = 1
      # 界说两个变量保留数据从哪女与到哪女
      data_start = (page_num - 1) * 10
      data_end = page_num * 10 
      # 页里上最多展现的页码
      max_page = 11
      half_max_page = max_page // 2
      # 页里上展现的页码的起头页
      page_start = page_num - half_max_page
      # 页里上展现的页码的完毕页
      page_end = page_num + half_max_page 
      # 若是以后页加1半比 1 小
      if page_start <= 1:
        page_start = 1
        page_end = max_page
      # 若是以后页减1半比总页码借年夜
      if page_end > total_page:
        page_end = total_page
        page_start = total_page - max_page + 1 
      all_book = models.Book.objects.all()[data_start:data_end] 
      # 拼接 html 的分页代码
      html_list = [] 
      # 增加尾页按钮
      html_list.append('
  • 尾页
  • ') # 若是是第1页,便出有上1页 if page_num <= 1: html_list.append('
  • '.format(page_num⑴)) else: # 减1个上1页的标签 html_list.append('
  • '.format(page_num⑴)) # 展现的页码 for i in range(page_start, page_end + 1): # 给以后页增加 active if i == page_num: tmp = '
  • {0}
  • '.format(i) else: tmp = '
  • {0}
  • '.format(i) html_list.append(tmp) # 若是是最初1页,便出有下1页 if page_num >= total_page: html_list.append('
  • ') else: html_list.append('
  • '.format(page_num+1)) # 增加尾页按钮 html_list.append('
  • 尾页
  • '.format(total_page)) page_html = "".join(html_list) # 拼接 html 的分页代码 return render(request, "book_list.html", {"books": all_book, "page_html": page_html})

    若是数据库中的数据数少于 max_page,则会显现背数的页数

    比方数据库中只要 21 条数据:

    改良:

    from django.shortcuts import render
    from app01 import models
    def book_list(request):
      # 从 URL 中与参数
      page_num = request.GET.get("page")
      print(page_num, type(page_num)) # page_num 为 str 范例 
      # 册本总数
      total_count = models.Book.objects.all().count() 
      # 每页显现几条数据
      per_page = 10 
      # 统共需求几页码去显现
      total_page, m = divmod(total_count, per_page) 
      # 若是借无数据
      if m:
        total_page += 1
      try:
        page_num = int(page_num)
        # 若是输出的页码数超越了最年夜的页码数,默许前往最初1页
        if page_num > total_page:
          page_num = total_page
        # 若是输出的页码数小于 1,则前往第1页
        if page_num < 1:
          page_num = 1
      except Exception as e:
        # 当输出的页码没有是端庄数字的时分 默许前往第1页的数据
        page_num = 1 
      # 界说两个变量保留数据从哪女与到哪女
      data_start = (page_num - 1) * 10
      data_end = page_num * 10 
      # 页里上最多展现的页码
      max_page = 11
      # 若是总页码数小于页里上最多展现的页码
      if total_page < max_page:
        max_page = total_page
      half_max_page = max_page // 2 
      # 页里上展现的页码的起头页
      page_start = page_num - half_max_page
      # 页里上展现的页码的完毕页
      page_end = page_num + half_max_page 
      # 若是以后页加1半比 1 小
      if page_start <= 1:
        page_start = 1
        page_end = max_page
      # 若是以后页减1半比总页码借年夜
      if page_end > total_page:
        page_end = total_page
        page_start = total_page - max_page + 1 
      all_book = models.Book.objects.all()[data_start:data_end] 
      # 拼接 html 的分页代码
      html_list = [] 
      # 增加尾页按钮
      html_list.append('
  • 尾页
  • ') # 若是是第1页,便出有上1页 if page_num <= 1: html_list.append('
  • '.format(page_num⑴)) else: # 减1个上1页的标签 html_list.append('
  • '.format(page_num⑴)) # 展现的页码 for i in range(page_start, page_end + 1): # 给以后页增加 active if i == page_num: tmp = '
  • {0}
  • '.format(i) else: tmp = '
  • {0}
  • '.format(i) html_list.append(tmp) # 若是是最初1页,便出有下1页 if page_num >= total_page: html_list.append('
  • ') else: html_list.append('
  • '.format(page_num+1)) # 增加尾页按钮 html_list.append('
  • 尾页
  • '.format(total_page)) page_html = "".join(html_list) # 拼接 html 的分页代码 return render(request, "book_list.html", {"books": all_book, "page_html": page_html})

    运转成果:

    以上便是本文的全数内容,期望对各人的进修有所帮忙,也期望各人多多撑持剧本之家。