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

APIView และฟังก์ชัน @api_view

สร้างเอนด์พอยต์ได้สองวิธี

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

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

Two Ways to Build Endpoints

DRF lets you write endpoints as classes with APIView or as plain functions decorated with @api_view. Both speak the same JSON dialect.

The Class Style

APIView subclasses Django's View but gives you DRF's request parsing, content negotiation, and tidy response handling for free.

from rest_framework.views import APIView

Methods Map to Verbs

In an APIView you write a method per HTTP verb: get for reads, post for creates, and so on, keeping each action clearly separated.

class BookList(APIView):
    def get(self, request):
        ...
    def post(self, request):
        ...

Return a Response

Instead of HttpResponse, you return DRF's Response object, which renders to JSON or the browsable API based on the request.

from rest_framework.response import Response
return Response({'ok': True})

Read request.data

DRF parses the body for you, so you read posted JSON from request.data as a normal Python dict, no manual decoding needed.

title = request.data['title']

The Function Style

For simple endpoints the @api_view decorator turns an ordinary function into a DRF view, listing which HTTP methods it accepts.

from rest_framework.decorators import api_view

Declare Allowed Methods

You pass the verbs to the decorator. Any method not listed gets an automatic 405 Method Not Allowed, so you do not handle it yourself.

@api_view(['GET', 'POST'])
def books(request):
    ...

Same Response Object

Function views use the very same DRF Response, so you can mix class and function styles in one project without friction.

return Response(serializer.data)

Which Style to Pick

Reach for @api_view when an endpoint is small and one-off; choose APIView when you want reusable, organized methods on a class.

Set the Status

Both styles let you pass status= to Response so clients get the right HTTP code, like 201 when you create something new.

Response(data, status=201)

Same Power, Two Shapes

Whether class or function, you get DRF parsing, responses, and status handling, the difference is mostly structure and personal taste.

Quick Check

What does @api_view do to a plain function?

Recap: Two View Styles

You can build endpoints with class-based APIView or function-based @api_view, both returning a DRF Response. Pick the shape that fits. 🧩

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

บทเรียน “APIView และฟังก์ชัน @api_view” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “APIView และฟังก์ชัน @api_view”

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

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

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

บทเรียน “APIView และฟังก์ชัน @api_view” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การติดตั้งและกำหนดค่า DRF
  2. ซีเรียลไลเซอร์: โมเดลเป็น JSON
  3. APIView และฟังก์ชัน @api_view
  4. API ที่เรียกดูได้และรหัสสถานะ
← กลับไปที่ Django Academy