فئة Paginator
تقسيم queryset إلى صفحات
فئة Paginator درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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. 🎉
الأسئلة الشائعة
هل درس «فئة Paginator» مجاني؟
نعم — نص درس «فئة Paginator» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «فئة Paginator»؟
تقسيم queryset إلى صفحات تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «فئة Paginator»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.