0Pricing
Django Academy · Lektion

Template-Vererbung mit extends und block

Erstellen Sie ein Basislayout und überschreiben Sie Abschnitte.

Template-Vererbung mit extends und block ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.html

Define 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.

Häufig gestellte Fragen

Ist die Lektion „Template-Vererbung mit extends und block“ kostenlos?

Ja — der vollständige Text von „Template-Vererbung mit extends und block“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Template-Vererbung mit extends und block“?

Erstellen Sie ein Basislayout und überschreiben Sie Abschnitte. Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Django Academy zu starten?

Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Template-Vererbung mit extends und block“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?

Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. render() und der Template-Kontext
  2. Template-Tags und Filter
  3. Template-Vererbung mit extends und block
  4. Das url-Tag und das static-Tag
← Zurück zu Django Academy