APIViewと関数ベースの@api_view
2通りの方法でエンドポイントを構築します
「APIViewと関数ベースの@api_view」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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 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. 🧩
よくある質問
「APIViewと関数ベースの@api_view」レッスンは無料ですか?
はい。「APIViewと関数ベースの@api_view」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「APIViewと関数ベースの@api_view」で何を学びますか?
2通りの方法でエンドポイントを構築します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「APIViewと関数ベースの@api_view」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- DRFのインストールと設定
- Serializers:ModelからJSONへ
- APIViewと関数ベースの@api_view
- Browsable APIとステータスコード