Class-Based Views
CBV vs FBV
Section titled “CBV vs FBV”| Feature | FBV (Function-Based) | CBV (Class-Based) |
|---|---|---|
| โครงสร้าง | function | class |
| HTTP methods | if/elif statements | แยก method ชัดเจน |
| Inheritance | ไม่มี | รองรับ |
| Mixins | ไม่มี | รองรับ |
| Readability | ง่ายสำหรับ simple views | ดีสำหรับ complex views |
| Reusability | Copy/paste | Inherit และ override |
เมื่อไหร่ใช้อะไร
Section titled “เมื่อไหร่ใช้อะไร”| Use Case | Recommendation |
|---|---|
| Simple views | FBV |
| CRUD operations | CBV (Generic Views) |
| Complex logic with inheritance | CBV |
| One-off custom logic | FBV |
| REST API | CBV + DRF ViewSets |
เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google