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