extendsとblockによるテンプレート継承
ベースレイアウトを作成し、セクションを上書きします
「extendsとblockによるテンプレート継承」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Stop Repeating Layout
Every page shares a header, nav, and footer. Template inheritance lets you write that shell once and reuse it everywhere. 🧱
Start with a Base
A base template holds the common HTML skeleton. Child pages will plug their unique content into spots you leave open.
base.htmlDefine a block
A block marks a section a child can replace. You name it, leave optional default content, then close it with endblock.
{% block content %}{% endblock %}A Minimal Base File
This base wraps a title block and a content block in real HTML. Children will fill those two named holes.
<html><head><title>{% block title %}Site{% endblock %}</title></head>
<body>{% block content %}{% endblock %}</body></html>Children Use extends
The extends tag must be the first line of a child template. It tells Django which base layout to build upon.
{% extends "base.html" %}Override a block
In the child you redefine a block with the same name. Whatever you put inside replaces the base default for that page.
{% extends "base.html" %}
{% block content %}<h1>Welcome</h1>{% endblock %}Unfilled Blocks Use Defaults
If a child skips a block, the base content shows instead. That makes a default a safe placeholder for shared text.
Keep Parent Content with super
Call block.super to keep the parent block content and add your own around it, instead of replacing it entirely.
{% block content %}{{ block.super }}<p>Extra</p>{% endblock %}Inheritance Can Nest
A child can itself be a base for deeper pages. Layered layouts let sections share a sub-shell while still using the global one.
extends Goes First
Only one extends is allowed and it must lead the file. Anything before it, even a comment, breaks the inheritance.
Blocks Build Pages
Think of a page as a base full of blocks that children fill. This keeps every page consistent and edits in one place.
Quick Check
Recall how a child connects to its layout.
Recap: Write Layout Once
You learned to define blocks in a base and fill them with extends in children, even keeping parent content via block.super. Next: linking routes and assets.
よくある質問
「extendsとblockによるテンプレート継承」レッスンは無料ですか?
はい。「extendsとblockによるテンプレート継承」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「extendsとblockによるテンプレート継承」で何を学びますか?
ベースレイアウトを作成し、セクションを上書きします ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「extendsとblockによるテンプレート継承」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- render()とテンプレートコンテキスト
- テンプレートタグとフィルター
- extendsとblockによるテンプレート継承
- urlタグとstaticタグ