0Pricing
Flask Academy · Lezione

Estendere il template base nelle pagine figlie

Utilizzi extends e riempia i blocchi per ogni pagina.

Estendere il template base nelle pagine figlie è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Connect a Page to the Base

A child page does not repeat the skeleton. It declares that it extends the base, then fills only the blocks it needs to change. 🔗

The extends Tag

The first line of a child template uses extends to name its parent layout. This tells Jinja2 to inherit the base before rendering.

{% extends "base.html" %}

extends Must Come First

The extends tag has to be the very first thing in the child file. Putting markup above it breaks inheritance and raises a template error.

Override a Block

To replace a slot, redeclare the block with the same name in the child. Whatever you put inside now shows instead of the base default.

{% block title %}Dashboard{% endblock %}

A Complete Child Page

This child template extends base.html and overrides two blocks. The rest of the page comes straight from the parent layout.

{% extends "base.html" %}
{% block title %}Home{% endblock %}
{% block content %}
  <h1>Welcome</h1>
{% endblock %}

Skip Blocks You Like

You only override the blocks you want to change. Any block you leave out keeps the default content defined in the base template.

Reuse Parent Content

Inside an overridden block you can call super() to pull in the parent block content, then add your own around it.

{% block content %}{{ super() }}<p>Extra</p>{% endblock %}

Render the Child, Not the Base

Your view returns the child template path. Flask renders the child, which inherits the base, so you never call base.html directly.

return render_template("home.html")

Block Names Must Match

Override only works when the child block name exactly matches one in the base. A typo silently leaves the parent default in place.

One Base, Many Children

Dozens of pages can extend the same base.html. Each child stays tiny because it only carries the markup unique to that page.

Inheritance Saves You

Change the base header once and every child page updates instantly. That cascade is the real power of template inheritance. 🚀

Quick Check

Where must the extends tag appear in a child template?

Recap: Extending Pages

Child pages extend a base, override matching blocks, and may reuse parent content with super(). One base now powers many pages. 🎯

Domande Frequenti

La lezione «Estendere il template base nelle pagine figlie» è gratuita?

Sì — il testo completo di «Estendere il template base nelle pagine figlie» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.

Cosa imparerò in «Estendere il template base nelle pagine figlie»?

Utilizzi extends e riempia i blocchi per ogni pagina. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flask Academy?

Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Estendere il template base nelle pagine figlie»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flask Academy?

Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Definire un template base con i blocchi
  2. Estendere il template base nelle pagine figlie
  3. Includere partial riutilizzabili
  4. Macro per markup ripetuto
← Torna a Flask Academy