0Pricing
Django Academy · レッスン

TemplateViewとget_context_data

テンプレートをレンダリングし、コンテキストをきれいに追加します

「TemplateViewとget_context_data」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Meet TemplateView

TemplateView is a generic CBV whose only job is to render a template. It is the simplest class-based view you will use.

Set template_name

You tell TemplateView which file to render with the template_name attribute. No get method needed for a plain page.

class AboutView(TemplateView):
    template_name = "about.html"

Wire It Into urls

Add it to urls with as_view(), just like any CBV. That one line gives you a fully working page.

path("about/", AboutView.as_view())

Passing Data to Templates

A static page is fine, but usually you need context: data your template can display. CBVs provide a clean hook for that.

Override get_context_data

The get_context_data method builds the dictionary passed to your template. Override it to add your own values.

Always Call super() First

Start by calling super() to get the default context, then add to it. Skipping this can drop data Django expects.

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    return context

Add Your Own Keys

After super(), insert any keys you need into the context dict. The template reads them by name.

context["year"] = 2024
return context

Templates Read Context Keys

In the template, reference a context key with double braces. Whatever you put under year appears where you write it.

kwargs Carries URL Data

The kwargs argument holds values captured from the URL, like an id. You can read them while building context.

Less Boilerplate Than Functions

Compared to a function view, TemplateView removes the repetitive render call. You declare the template once and add only the data.

A Common First CBV

TemplateView plus get_context_data is the pattern you will reach for whenever a page needs a template and a bit of dynamic data.

Quick Check

Which method do you override to add data for a TemplateView?

Recap: TemplateView

You learned TemplateView: set template_name to render a page, and override get_context_data, calling super first, to pass data along. ✨

よくある質問

「TemplateViewとget_context_data」レッスンは無料ですか?

はい。「TemplateViewとget_context_data」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。

「TemplateViewとget_context_data」で何を学びますか?

テンプレートをレンダリングし、コンテキストをきれいに追加します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Django Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「TemplateViewとget_context_data」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDjango Academyレッスンでコードを書いて実行できますか?

はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 関数ビューからビュークラスへ
  2. TemplateViewとget_context_data
  3. getメソッドとpostメソッドの処理
  4. Mixinsとas_view()
← Django Academyに戻る