这是我参与更文挑战的第10天,活动详情查看: 更文挑战
数据分页原理
- 1.拿到page => str
- 2.获取数据
User.objects.all() - 3.对数据进行处理,分页,根据page计算切片的start与end
queryset = User.objects.all()[start:end] - 4.转json
ret = list(queryset.values(“id”,”username”,”email”)) - 5.HttpResponse
Paginator和Page类
paginator和page类都是用来做分页的,他们在Django中的路径为django.core.paginator和django.core.paginator.page
paginator常用属性和方法
- 1、count:总共有多少条数据
- 2、num_pages:总共有多少页
- 3、page_range:页面的区别,比如有3页就是range(1, 4)
page常用属性和方法
- 1、has_next:是否还有下一页
- 2、has_previous:是否还有上一页
- 3、next_page_number:下一页的页码
- 4、previous_page_number:上一页的页码
- 5、number:当前页
- 6、start_index:当前这一页的第一条数据的索引值
- 7、end_index:当前这一页的最后一条数据的索引值
bootstrap前端页面
<!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 class="pagination">
{# 上一页 #}
{% if page_obj.has_previous %}
<li><a href="{% url 'front:article_list' %}?p={{ page_obj.previous_page_number }}">上一页</a></li>
{% else %}
<li class="disabled"><a href="javascript:void(0);">上一页</a></li>
{% endif %}
{% if left_has_more %}
<li><a href="{% url 'front:article_list' %}?p=1">1</a></li>
<li><a href="javascript:void(0);">...</a></li>
{% endif %}
{# 中间页,左边页码 #}
{% for left_page in left_pages %}
<li><a href="{% url 'front:article_list' %}?p={{ left_page }}">{{ left_page }}</a></li>
{% endfor %}
{# 当前页面 #}
<li class="active"><a href="{% url 'front:article_list' %}?p={{ current_page }}">{{ current_page }}</a></li>
{# 右边页面 #}
{% for right_page in right_pages %}
<li><a href="{% url 'front:article_list' %}?p={{ right_page }}">{{ right_page }}</a></li>
{% endfor %}
{% if right_has_more %}
<li><a href="javascript:void(0);">...</a></li>
<li><a href="{% url 'front:article_list' %}?p={{ num_pages }}">{{ num_pages }}</a></li>
{% endif %}
{# 下一页 #}
{% if page_obj.has_next %}
<li><a href="{% url 'front:article_list' %}?p={{ page_obj.next_page_number }}">下一页</a></li>
{% else %}
<li class="disabled"><a href="javascript:void(0);">下一页</a></li>
{% endif %}
</ul>
</ul>
</body>
</html>
后端代码
# views.py
from django.http import HttpResponse
from .models import Article
from django.views.generic import ListView
def add_article(request):
articles = []
for i in range(0,102):
article = Article(title='标题:%s' %i, content='内容:content_%s' %i)
articles.append(article)
Article.objects.bulk_create(articles)
return HttpResponse('article add success!')
class ArticleListView(ListView):
model = Article
template_name = 'article_list.html'
context_object_name = 'articles'
paginate_by = 10
ordering = 'create_time'
page_kwarg = 'p'
def get_context_data(self, **kwargs):
context = super(ArticleListView, self).get_context_data(*kwargs)
context['username'] = 'liyk'
paginator = context.get('paginator')
page_obj = context.get('page_obj')
print(paginator.num_pages)
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=1):
current_page = page_obj.number
num_pages = paginator.num_pages
left_has_more = False
right_has_more = False
if current_page <= around_count + 2:
left_pages = range(1,current_page)
else:
left_has_more = True
left_pages = range(current_page-around_count, current_page)
if current_page >= num_pages - around_count - 1:
right_pages = range(current_page+1, num_pages+1)
else:
right_has_more = True
right_pages = range(current_page+1, current_page+around_count+1)
return {
'left_pages': left_pages,
'right_pages': right_pages,
'current_page': current_page,
'left_has_more': left_has_more,
'right_has_more': right_has_more,
'num_pages': num_pages
}
# urls.py
from django.urls import path
from . import views
app_name = 'front'
urlpatterns = [
path('add/', views.add_article, name='add_article'),
path('list/', views.ArticleListView.as_view(),name='article_list'),
]
# models.py
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
create_time = models.DateTimeField(auto_now_add=True)
效果图
今天的文章Django分页显示列表分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/17840.html