查詢
分頁器 - django-pure-pagination
官網: https://github.com/jamespacileo/django-pure-pagination
INSTALLED_APPS = [
'pure_pagination', # 分頁器
... ...
views.py
from django.db.models import Q
from django.views import generic
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger,PaginationMixin
# 基於類視圖
class SearchView(PaginationMixin, generic.ListView):
model = Article
template_name = "search.html"
paginate_by = 10
# 返回查詢記錄
def get_queryset(self):
key = self.request.GET.get('q')
if key:
context = Article.objects.filter(Q(title__icontains=key)|Q(body__icontains=key)).order_by('-click_count')
return context
# 基於函數視圖
def search(request):
try:
page = request.GET.get('page', 1)
except PageNotAnInteger:
page = 1
key = request.GET.get('q')
if key:
article_list = Article.objects.filter(Q(title__icontains=key) | Q(body__icontains=key)).order_by('-click_count')
p = Paginator(article_list,5, request=request)
page_obj = p.page(page)
return render(request,'search.html', {
# 'article_list': article_list,
'page_obj': page_obj,
})
urls.py
path('search/', SearchView.as_view(), name='search_view'), # 類視圖搜索
path('search2/', search, name='search'), # 類視圖搜索
search.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h2> 搜索:</h2>
<ul>
{% for article in page_obj.object_list %}
<li>
<div class="description center-align post-title">
<a href="{% url 'detail' article.id %}" title="Others: 29"> {{ article.title }}
</a>
</div>
</li>
{% empty %}
<p>No results found.</p>
{% endfor %}
</ul>
{# The following renders the pagination html #}
{% include "pagination.html" %}
</body>
</html>
pagination.html
{% load i18n %}
<div class="pagination">
{% if page_obj.has_previous %}
<a href="?{{ page_obj.previous_page_number.querystring }}" class="prev">‹‹ {% trans "previous" %}</a>
{% else %}
<span class="disabled prev">‹‹ {% trans "previous" %}</span>
{% endif %}
{% for page in page_obj.pages %}
{% if page %}
{% ifequal page page_obj.number %}
<span class="current page">{{ page }}</span>
{% else %}
<a href="?{{ page.querystring }}" class="page">{{ page }}</a>
{% endifequal %}
{% else %}
...
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a href="?{{ page_obj.next_page_number.querystring }}" class="next">{% trans "next" %} ››</a>
{% else %}
<span class="disabled next">{% trans "next" %} ››</span>
{% endif %}
</div>
Django分頁方法和分頁的使用
django開發了一個獨特的插件paginator專門負責分頁
常用方法
1.paginator.count 總記錄數據量
2.paginator.page_range 顯示的是頁數的範圍,就是按鈕的數目range(1,21)
3.paginator.num_pages 分頁數
4.page_obj = paginator.page(1) page_obj表示當前對象,獲取第一頁所有記錄
5.page_obj.has_next() 是否有下一頁True
6.page_obj.has_previous() 是否有上一頁False
7.page_obj.has_other_pages() 是否還有頁True
8.page_obj.next_page_number() 下一頁頁碼,如果下一頁不存在報錯
9.page_obj.previous_page_number() 上一頁頁碼,如果上一頁不存在報錯
from django.core.paginator import Paginator,EmptyPage
def fy_test(request):
book_obj_list = models.Books.objects.all()
paginator = Paginator(book_obj_list,5) # 設置每一頁顯示5條數據,創建一個paginator對象
print(paginator.count) # 總數據量100
print(paginator.page_range) # 顯示的是頁數的範圍,range(1,21)
print(paginator.num_pages) # 分頁數20
獲取某一頁的數據
page_obj = paginator.page(18) # page_obj表示當前對象
print(page_obj) # <Page 18 of 20>
print(page_obj.has_next()) # 是否有下一頁 True
print(page_obj.has_previous()) # 是否有上一頁 Ture
print(page_obj.has_other_pages()) # 是否還有頁True
print(page_obj.next_page_number()) # 下一頁頁碼,如果下一頁不存在報錯
print(page_obj.previous_page_number()) # 上一頁頁碼,如果上一頁不存在報錯
return HttpResponse(‘批量添加完成’)
from django.core.paginator import Paginator,EmptyPage
def type_list(request):
goods_type_obj_list = models.GoodsType.objects.all()
my_num = 5
paginator = Paginator(goods_type_obj_list, my_num)
num_pages = paginator.num_pages # 分頁數、總頁碼數
count = paginator.count # 總記錄數
try:
current_num = int(request.GET.get(‘page’,1)) # 設置默認值爲1
page_obj = paginator.page(current_num)
except Emptypage:
page_obj = paginator.page(1)
return render(request, ‘seller/type_list.html’, locals())