Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
407c1249
Kaydet (Commit)
407c1249
authored
Nis 06, 2017
tarafından
Sami J. Lehtinen
Kaydeden (comit)
Tim Graham
Eyl 06, 2017
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #28032 -- Added Paginator.get_page().
Moved boilerplate from docs to a method.
üst
5b1c3896
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
9 deletions
+66
-9
paginator.py
django/core/paginator.py
+13
-0
2.0.txt
docs/releases/2.0.txt
+6
-0
pagination.txt
docs/topics/pagination.txt
+15
-9
tests.py
tests/pagination/tests.py
+32
-0
No files found.
django/core/paginator.py
Dosyayı görüntüle @
407c1249
...
...
@@ -47,6 +47,19 @@ class Paginator:
raise
EmptyPage
(
_
(
'That page contains no results'
))
return
number
def
get_page
(
self
,
number
):
"""
Return a valid page, even if the page argument isn't a number or isn't
in range.
"""
try
:
number
=
self
.
validate_number
(
number
)
except
PageNotAnInteger
:
number
=
1
except
EmptyPage
:
number
=
self
.
num_pages
return
self
.
page
(
number
)
def
page
(
self
,
number
):
"""Return a Page object for the given 1-based page number."""
number
=
self
.
validate_number
(
number
)
...
...
docs/releases/2.0.txt
Dosyayı görüntüle @
407c1249
...
...
@@ -290,6 +290,12 @@ Models
* Added support for expressions in :attr:`Meta.ordering
<django.db.models.Options.ordering>`.
Pagination
~~~~~~~~~~
* Added :meth:`Paginator.get_page() <django.core.paginator.Paginator.get_page>`
to provide the documented pattern of handling invalid page numbers.
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~
...
...
docs/topics/pagination.txt
Dosyayı görüntüle @
407c1249
...
...
@@ -93,15 +93,7 @@ The view function looks like this::
paginator = Paginator(contact_list, 25) # Show 25 contacts per page
page = request.GET.get('page')
try:
contacts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)
contacts = paginator.get_page(page)
return render(request, 'list.html', {'contacts': contacts})
In the template :file:`list.html`, you'll want to include navigation between
...
...
@@ -177,6 +169,20 @@ Optional arguments
Methods
-------
.. method:: Paginator.get_page(number)
.. versionadded:: 2.0
Returns a :class:`Page` object with the given 1-based index, while also
handling out of range and invalid page numbers.
If the page isn't a number, it returns the first page. If the page number
is negative or greater than the number of pages, it returns the last page.
It raises an exception (:exc:`EmptyPage`) only if you specify
``Paginator(..., allow_empty_first_page=False)`` and the ``object_list`` is
empty.
.. method:: Paginator.page(number)
Returns a :class:`Page` object with the given 1-based index. Raises
...
...
tests/pagination/tests.py
Dosyayı görüntüle @
407c1249
...
...
@@ -242,6 +242,38 @@ class PaginationTests(unittest.TestCase):
"""
self
.
assertIsInstance
(
Paginator
([
1
,
2
,
3
],
2
)
.
page_range
,
type
(
range
(
0
)))
def
test_get_page
(
self
):
"""
Paginator.get_page() returns a valid page even with invalid page
arguments.
"""
paginator
=
Paginator
([
1
,
2
,
3
],
2
)
page
=
paginator
.
get_page
(
1
)
self
.
assertEqual
(
page
.
number
,
1
)
self
.
assertEqual
(
page
.
object_list
,
[
1
,
2
])
# An empty page returns the last page.
self
.
assertEqual
(
paginator
.
get_page
(
3
)
.
number
,
2
)
# Non-integer page returns the first page.
self
.
assertEqual
(
paginator
.
get_page
(
None
)
.
number
,
1
)
def
test_get_page_empty_object_list
(
self
):
"""Paginator.get_page() with an empty object_list."""
paginator
=
Paginator
([],
2
)
# An empty page returns the last page.
self
.
assertEqual
(
paginator
.
get_page
(
1
)
.
number
,
1
)
self
.
assertEqual
(
paginator
.
get_page
(
2
)
.
number
,
1
)
# Non-integer page returns the first page.
self
.
assertEqual
(
paginator
.
get_page
(
None
)
.
number
,
1
)
def
test_get_page_empty_object_list_and_allow_empty_first_page_false
(
self
):
"""
Paginator.get_page() raises EmptyPage if allow_empty_first_page=False
and object_list is empty.
"""
paginator
=
Paginator
([],
2
,
allow_empty_first_page
=
False
)
with
self
.
assertRaises
(
EmptyPage
):
paginator
.
get_page
(
1
)
class
ModelPaginationTests
(
TestCase
):
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment