可浏览 API 与状态码
在浏览器中正确测试端点
可浏览 API 与状态码 是 CoddyKit 上的免费 Django Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Test in the Browser
DRF's browsable API renders any endpoint as an interactive web page, so you can explore and test your API without extra tools. 🌐
How It Decides
DRF uses content negotiation: browsers ask for HTML and get the friendly page, while code asking for JSON gets raw JSON instead.
Forms for Free
For write endpoints the browsable API even shows a form, letting you POST test data with a click instead of crafting curl commands.
Force JSON Anytime
Append ?format=json to any URL to skip the HTML page and see the exact JSON a real client would receive.
GET /api/books/?format=jsonStatus Codes Matter
Every API response carries an HTTP status code that tells the client what happened, far more reliably than reading the body text.
200 and 201
200 means a successful read, while 201 Created signals that a POST actually made a new resource, a small but important distinction.
Readable Constants
Instead of magic numbers, DRF gives you named constants in status so your code reads clearly and avoids typos like 20 versus 200.
from rest_framework import status
status.HTTP_201_CREATEDClient Errors: 400 and 404
The 4xx range blames the request: 400 means bad input that failed validation, and 404 means the resource simply was not found.
Response(serializer.errors, status=400)Server Errors: 500
A 500 means your code crashed, not the client. These signal bugs to fix, never something the caller did wrong.
Match Code to Outcome
Good APIs are predictable: return 201 on create, 400 on invalid input, 404 when missing. Correct status codes make clients trust you.
Browsable Plus Status
Together the browsable API and clear status codes make DRF endpoints easy to test by hand and easy for clients to consume.
Quick Check
Which status code best fits a successful POST that creates a record?
Recap: Test and Respond
You met the browsable API for hands-on testing and learned to return honest status codes like 200, 201, 400, and 404. ✅
常见问题解答
「可浏览 API 与状态码」课时是免费的吗?
是的 — 「可浏览 API 与状态码」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。
「可浏览 API 与状态码」这节课中我会学到什么?
在浏览器中正确测试端点 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Django Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「可浏览 API 与状态码」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Django Academy 课中编写并运行代码吗?
能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 安装和配置 DRF
- 序列化器:从模型到 JSON
- APIView 与函数式 @api_view
- 可浏览 API 与状态码