0Pricing
Django Academy · 课时

安装和配置 DRF

添加 DRF 和可浏览 API

安装和配置 DRF 是 CoddyKit 上的免费 Django Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

A REST API, Quickly

Django REST Framework, or DRF, layers a clean toolkit on top of Django so you can turn your models into a JSON API fast. 🚀

Install the Package

You add DRF the same way you add any Python library: with pip, then pin it in requirements so teammates get the same version.

pip install djangorestframework

Register the App

DRF only wakes up once you add rest_framework to your INSTALLED_APPS list in settings.py, right alongside your own apps.

INSTALLED_APPS = [
    # ...
    'rest_framework',
]

Why INSTALLED_APPS Matters

Adding it to INSTALLED_APPS lets Django discover DRF's templates, static files, and management commands automatically.

The REST_FRAMEWORK Dict

You configure DRF through a single REST_FRAMEWORK dictionary in settings.py, keeping all of its options in one tidy place.

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [],
}

Default Renderers

By default DRF can return both raw JSON and a friendly HTML view, thanks to its built-in renderers you rarely have to touch.

The Browsable API

One reason DRF feels magical is the browsable API: open an endpoint in your browser and you get a clickable, documented page. ✨

Wire Up the URLs

DRF ships login views for the browsable API; you include them in your urls.py so you can sign in while testing endpoints.

urlpatterns = [
    path('api-auth/', include('rest_framework.urls')),
]

Verify the Install

A quick way to confirm DRF is wired in is to open the Django shell and import it without errors before writing any code.

python manage.py shell
>>> import rest_framework

Keep Settings Minimal

Start with an almost empty config. You only add keys to REST_FRAMEWORK when you genuinely need a behavior changed, not before.

You Are API-Ready

With DRF installed, registered, and its URLs included, your project is now ready to expose endpoints that speak JSON to any client.

Quick Check

Where does DRF need to be listed to activate?

Recap: DRF Set Up

You pip-installed DRF, added it to INSTALLED_APPS, included its auth URLs, and confirmed the import. Your API foundation is solid. 🎉

常见问题解答

「安装和配置 DRF」课时是免费的吗?

是的 — 「安装和配置 DRF」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。

「安装和配置 DRF」这节课中我会学到什么?

添加 DRF 和可浏览 API 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「安装和配置 DRF」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Django Academy 课中编写并运行代码吗?

能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 安装和配置 DRF
  2. 序列化器:从模型到 JSON
  3. APIView 与函数式 @api_view
  4. 可浏览 API 与状态码
← 返回 Django Academy