0Pricing
Django Academy · บทเรียน

ตัวควบคุมหน้าใน ListView

เพิ่มลิงก์ถัดไปและก่อนหน้าในเทมเพลต

ตัวควบคุมหน้าใน ListView เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัวควบคุมหน้าใน ListView”

เพิ่มลิงก์ถัดไปและก่อนหน้าในเทมเพลต คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “ตัวควบคุมหน้าใน ListView” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. คลาส Paginator
  2. ตัวควบคุมหน้าใน ListView
  3. การเขียน TestCase และคำยืนยัน
  4. ไคลเอนต์ทดสอบและฟิกซ์เจอร์
← กลับไปที่ Django Academy