APIView 与函数式 @api_view
用两种方式构建端点
APIView 与函数式 @api_view 是 CoddyKit 上的免费 Django Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。
「APIView 与函数式 @api_view」这节课中我会学到什么?
用两种方式构建端点 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Django Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「APIView 与函数式 @api_view」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Django Academy 课中编写并运行代码吗?
能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 安装和配置 DRF
- 序列化器:从模型到 JSON
- APIView 与函数式 @api_view
- 可浏览 API 与状态码