0Pricing
Django Academy · درس

عناصر التحكم بالصفحات في ListView

إضافة روابط التالي والسابق في القوالب

عناصر التحكم بالصفحات في ListView درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Pagination Built In

Good news: ListView can paginate for you. You barely write any code, and Django handles slicing the queryset into pages automatically. 🚀

Set paginate_by

Just add one attribute. Setting paginate_by tells the ListView how many items each page should show.

class ArticleList(ListView):
    model = Article
    paginate_by = 10

The page_obj Variable

With pagination on, your template receives a page_obj. It is the same Page object you met before, ready to loop and inspect.

Looping the Current Page

In the template, iterate page_obj to show only this page's items. Django already sliced the queryset for you.

{% for article in page_obj %}
  {{ article.title }}
{% endfor %}

Reading the Page Number

ListView pulls the page number from the ?page= query string in the URL, so a link to ?page=2 shows the second page.

Show a Previous Link

Guard the back link with has_previous so it only appears when there really is an earlier page to visit.

{% if page_obj.has_previous %}
  Prev
{% endif %}

Link to the Previous Page

Inside that block, build the href using previous_page_number so the link points one page back.

<a href="?page={{ page_obj.previous_page_number }}">Prev</a>

Show a Next Link

Mirror the logic for forward navigation. Wrap the next link in a has_next check so it hides on the final page.

{% if page_obj.has_next %}
  Next
{% endif %}

Display Page X of Y

Reassure users with a counter. Combine number and the paginator's num_pages to print "Page 2 of 5".

Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}

Custom Context Name

Prefer a friendlier loop variable? Set context_object_name and Django names the page list whatever you choose.

context_object_name = 'articles'

Putting It Together

One attribute plus a small template block gives you full page controls: prev, next, and a position counter, with no view logic to write.

Quick Check

Your ListView template needs the current page's items.

Recap

You enabled pagination with paginate_by, looped page_obj, and built prev/next links guarded by has_previous and has_next. Clean lists, zero view code. 🎉

الأسئلة الشائعة

هل درس «عناصر التحكم بالصفحات في ListView» مجاني؟

نعم — نص درس «عناصر التحكم بالصفحات في ListView» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «عناصر التحكم بالصفحات في ListView»؟

إضافة روابط التالي والسابق في القوالب تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟

لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «عناصر التحكم بالصفحات في ListView»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟

نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. فئة Paginator
  2. عناصر التحكم بالصفحات في ListView
  3. كتابة TestCase وعبارات التحقق
  4. Test Client و Fixtures
← العودة إلى Django Academy