Paginator Sınıfı
Bir sorgu kümesini sayfalara bölün.
Paginator Sınıfı, CoddyKit'te ücretsiz bir Django Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Django Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Django Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Why Paginate?
Showing 5,000 rows on one page is slow and overwhelming. Pagination splits a long list into bite-sized pages your users can browse. 📄
Meet the Paginator
Django ships a Paginator class that does the math for you. You hand it a list and a page size, and it carves the data into pages.
from django.core.paginator import PaginatorCreate a Paginator
Pass your full queryset and how many items per page. Here every page holds 10 articles.
articles = Article.objects.all()
paginator = Paginator(articles, 10)Counting the Pages
Ask the paginator how many pages exist with num_pages. It rounds up, so 25 items at 10 per page gives 3 pages.
paginator.num_pages # 3Grab One Page
Call get_page() with a page number to fetch a single slice. It returns a Page object holding just those items.
page = paginator.get_page(2)Looping Over a Page
A Page is iterable, so you loop it like a list. You only ever touch the items on that one page, never all 5,000.
for article in page:
print(article.title)get_page Is Forgiving
If someone passes page 999 or the word "abc", get_page() quietly returns the last or first valid page instead of crashing.
Knowing Where You Are
The page knows its own spot. Read number for the current page, handy for highlighting it in your navigation bar.
page.number # 2Next and Previous
Use has_next() and has_previous() to decide whether to show the next or back link in your template.
page.has_next()
page.has_previous()The Neighboring Pages
Once you know there is a next page, next_page_number() gives you the number to link to. previous_page_number() does the same backward.
page.next_page_number() # 3Wiring It Into a View
In a view, read the requested page from the query string, then pass the page to your template context.
num = request.GET.get('page')
page = paginator.get_page(num)Quick Check
You have 23 items and a page size of 10. Let's test the math.
Recap
You met the Paginator: feed it a queryset and a page size, fetch slices with get_page(), and read num_pages, number, and has_next() to build navigation. 🎉
Sıkça Sorulan Sorular
“Paginator Sınıfı” dersi ücretsiz mi?
Evet — “Paginator Sınıfı” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Django Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Django Academy kursu toplamda 4 dersten oluşur.
“Paginator Sınıfı” dersinde ne öğreneceğim?
Bir sorgu kümesini sayfalara bölün. Django Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Django Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Django Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“Paginator Sınıfı” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Django Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Django Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Paginator Sınıfı
- ListView'da Sayfa Denetimleri
- TestCase ve Doğrulamalar Yazma
- Test İstemcisi ve Test Verileri