0Pricing
Django Academy · 课时

从函数视图到视图类

了解 CBV 存在的原因以及适用时机

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

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

Two Ways to Build Views

So far your views were plain functions. Django also lets you write views as classes, which package related logic together neatly.

What a Class-Based View Is

A class-based view (CBV) is just a Python class that handles a request. Django turns it into a callable for you behind the scenes.

The Repetition Problem

Function views often repeat the same shape: fetch data, build context, render a template. CBVs let you reuse that shape instead of copying it.

A Function View, For Contrast

Here is a familiar function view. It takes a request and returns a response, all in one block.

def home(request):
    return render(request, "home.html")

The Same View as a Class

As a class, you subclass View and define a method per HTTP verb. The logic is the same, just organized differently.

class HomeView(View):
    def get(self, request):
        return render(request, "home.html")

Methods Map to HTTP Verbs

In a CBV, a get method handles GET requests and a post method handles POST. No more giant if-checks on request.method.

Inheritance Means Reuse

Because CBVs are classes, you can subclass and override only what changes. Shared behavior lives in a parent class once.

Django Ships Generic Views

Django includes ready-made generic views like ListView and DetailView. They cover common patterns so you write almost no code.

When Functions Still Win

For a tiny, one-off view, a function is often clearer. Reach for CBVs when logic repeats or grows complex.

Both Plug Into urls.py

Both styles get wired the same way in urls. A CBV just needs as_view() to become a callable Django can route to.

path("", HomeView.as_view())

Pick the Right Tool

CBVs are not better or worse, just different. Choose based on reuse: repeated patterns favor classes, simple pages favor functions.

Quick Check

What is the main reason class-based views exist?

Recap: Functions vs Classes

You met class-based views: classes that handle requests, map methods to HTTP verbs, and reuse logic through inheritance. Great when patterns repeat. 🎯

常见问题解答

「从函数视图到视图类」课时是免费的吗?

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

「从函数视图到视图类」这节课中我会学到什么?

了解 CBV 存在的原因以及适用时机 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

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

「从函数视图到视图类」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 从函数视图到视图类
  2. TemplateView 与 get_context_data
  3. 处理 get 与 post 方法
  4. 混入类与 as_view()
← 返回 Django Academy