APIView dan Fungsi @api_view
Bangun endpoint dengan dua cara
APIView dan Fungsi @api_view adalah pelajaran Django Academy gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Django Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Django Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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 APIViewMethods 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_viewDeclare 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. 🧩
Pertanyaan yang Sering Diajukan
Apakah pelajaran “APIView dan Fungsi @api_view” gratis?
Ya — teks lengkap “APIView dan Fungsi @api_view” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Django Academy, upgrade ke CoddyKit PRO. Kursus Django Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “APIView dan Fungsi @api_view”?
Bangun endpoint dengan dua cara Kamu berlatih Django Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Django Academy?
Tidak diperlukan pengalaman sebelumnya. Django Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.
Berapa lama pelajaran “APIView dan Fungsi @api_view” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Django Academy ini?
Ya. Setiap pelajaran Django Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Memasang dan Mengonfigurasi DRF
- Serializer: Model ke JSON
- APIView dan Fungsi @api_view
- API yang Dapat Dijelajahi dan Kode Status