django创建分页

前台html代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<ul>
{% for article in articles %}
<li>{{ article.title }}</li>
{% endfor %}
</ul>
<nav aria-label="Page navigation">
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="{% url 'viewlist' %}?page={{ page_obj.previous_page_number }}">上一页</a></li>
{% else %}
<li class="disabled"><a>上一页</a></li>
{% endif %} {# 第一页#}
{% if current_page > 3 %}
<li><a href="{% url 'viewlist' %}?page=1">1</a></li>
{% endif %} {# 左边页码 #}
{% for left_page in left_pages %}
{% if left_page > 0 %}
<li><a href="{% url 'viewlist' %}?page={{ left_page }}">{{ left_page }}</a></li>
{% endif %}
{% endfor %}
<li><a href="javascript:void(0);">...</a></li>
{# 当前页码 #}
<li class="active"><a href="{% url 'viewlist' %}?page={{ current_page }}">{{ current_page }}</a></li>
{# 右边页码 #}
{% for right_page in right_pages %}
{% if right_page <= paginator.num_pages %}
<li><a href="{% url 'viewlist' %}?page={{ right_page }}">{{ right_page }}</a></li>
{% endif %}
{% endfor %}
<li><a href="javascript:void(0);">...</a></li>
{# 最后一页#}
{% if current_page < 98 %}
<li><a href="{% url 'viewlist' %}?page={{ paginator.num_pages }}">{{ paginator.num_pages }}</a></li>
{% endif %} {% if page_obj.has_next %}
<li><a href="{% url 'viewlist' %}?page={{ page_obj.next_page_number }}">下一页</a></li>
{% else %}
<li class="disabled"><a href="javascript:void(0);">下一页</a></li>
{% endif %} </ul>
</nav> </body>
</html>

后台view视图

from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic import ListView
from django.views.generic.base import TemplateView
from .models import Article, User, Book
from django.utils.timezone import now, localtime,make_aware
from datetime import datetime
from django.db.models import Count, Avg, F
from django.db.models.manager import Manager
from django.views import View class Index(View):
def get(self, request):
# info_list = []
# for i in range(1000):
# info_list.append(Article(title='title-%s' % i,content='content-%s' % i))
# Article.objects.bulk_create(info_list)
return HttpResponse('ok') class ArticleListView(ListView):
model = Article
template_name = 'article_list_view.html'
paginate_by = 10
context_object_name = 'articles'
ordering = 'id'
page_kwarg = 'page' def get_context_data(self, *, object_list=None, **kwargs):
context = super(ArticleListView, self).get_context_data(**kwargs)
page_obj = context.get('page_obj')
paginator = context.get('paginator')
pagination_data = self.get_pagination_data(paginator, page_obj)
context.update(pagination_data) return context def get_pagination_data(self,paginator,page_obj,around_count=2):
current_page = page_obj.number
left_page = range(current_page-around_count, current_page)
right_page = range(current_page+1,current_page+around_count+1)
return {
'current_page':current_page,
'left_pages': left_page,
'right_pages': right_page
}

URLS内容

from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
# path('', views.index, name='index')
# path('index/<book_id>/', views.Index.as_view(), name='index')
path('', views.Index.as_view(), name='index'),
path('viewlist/', views.ArticleListView.as_view(), name='viewlist'),
# path('viewlist/<int:book_id>', views.ArticleListView.as_view(), name='viewlist'),
]
上一篇:RabbitMQ(五) -- topics


下一篇:关于JavaScript定时器我的一些小理解