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

จากวิวแบบฟังก์ชันสู่วิวแบบคลาส

เหตุใดจึงมี CBV และควรใช้เมื่อใด

จากวิวแบบฟังก์ชันสู่วิวแบบคลาส เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Two Ways to Build Views

So far your views were plain functions. Django also lets you write views as classes, which package related logic together neatly.

What a Class-Based View Is

A class-based view (CBV) is just a Python class that handles a request. Django turns it into a callable for you behind the scenes.

The Repetition Problem

Function views often repeat the same shape: fetch data, build context, render a template. CBVs let you reuse that shape instead of copying it.

A Function View, For Contrast

Here is a familiar function view. It takes a request and returns a response, all in one block.

def home(request):
    return render(request, "home.html")

The Same View as a Class

As a class, you subclass View and define a method per HTTP verb. The logic is the same, just organized differently.

class HomeView(View):
    def get(self, request):
        return render(request, "home.html")

Methods Map to HTTP Verbs

In a CBV, a get method handles GET requests and a post method handles POST. No more giant if-checks on request.method.

Inheritance Means Reuse

Because CBVs are classes, you can subclass and override only what changes. Shared behavior lives in a parent class once.

Django Ships Generic Views

Django includes ready-made generic views like ListView and DetailView. They cover common patterns so you write almost no code.

When Functions Still Win

For a tiny, one-off view, a function is often clearer. Reach for CBVs when logic repeats or grows complex.

Both Plug Into urls.py

Both styles get wired the same way in urls. A CBV just needs as_view() to become a callable Django can route to.

path("", HomeView.as_view())

Pick the Right Tool

CBVs are not better or worse, just different. Choose based on reuse: repeated patterns favor classes, simple pages favor functions.

Quick Check

What is the main reason class-based views exist?

Recap: Functions vs Classes

You met class-based views: classes that handle requests, map methods to HTTP verbs, and reuse logic through inheritance. Great when patterns repeat. 🎯

คำถามที่พบบ่อย

บทเรียน “จากวิวแบบฟังก์ชันสู่วิวแบบคลาส” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “จากวิวแบบฟังก์ชันสู่วิวแบบคลาส” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “จากวิวแบบฟังก์ชันสู่วิวแบบคลาส”

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

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

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

บทเรียน “จากวิวแบบฟังก์ชันสู่วิวแบบคลาส” ใช้เวลานานแค่ไหน

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

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

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

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

  1. จากวิวแบบฟังก์ชันสู่วิวแบบคลาส
  2. TemplateView และ get_context_data
  3. จัดการเมธอด get และ post
  4. มิกซ์อินและ as_view()
← กลับไปที่ Django Academy